【问题标题】:Javascript nested JSON merge specific object name value as stringJavascript嵌套JSON将特定对象名称值合并为字符串
【发布时间】:2019-07-16 13:36:29
【问题描述】:

使用原版 Javascript,我想在下面的嵌套 JSON 示例中将多个位置的城市名称作为字符串返回。 由于 JSON 结构的复杂性(我无法更改)导致单独的循环,因此使用 push 不起作用。

"locations":[
    {
      "location":{
          "city":"New place 1",
          "other":"random"
      }
    },
    {
      "location":{
          "city":"New place 2",
          "other":"random dom"
      }
    }
]

预期输出: “新地点 1”、“新地点 2”

提前致谢。

【问题讨论】:

标签: javascript json


【解决方案1】:

你可以使用.map():

let locations = [
    {
      "location":{
          "city":"New place 1",
          "other":"random"
      }
    },
    {
      "location":{
          "city":"New place 2",
          "other":"random dom"
      }
    }
];

const result = locations.map(loc => loc.location.city);

console.log(result);
console.log(result.join(','));

【讨论】:

  • 非常感谢。像魅力一样工作。
【解决方案2】:

使用Array.mapArray.join

let locations = [{"location":{"city":"New place 1","other":"random"}},{"location":{"city":"New place 2","other":"random dom"}}];

let result = locations.map(v => '"' + v.location.city + '"').join(", ");
console.log(result);

【讨论】:

    【解决方案3】:

    或者,更简单的解决方案。

    let locations = [
        {
          "location":{
              "city":"New place 1",
              "other":"random"
          }
        },
        {
          "location":{
              "city":"New place 2",
              "other":"random dom"
          }
        }
    ];
    
    let Array = [];
    locations.forEach(function(par){
      Array.push(par.location.city);
    })
    
    let str = "";
    Array.forEach(function(par){
      str += par +", ";
    });
    
    str = str.slice(0,-2);
      
    
    console.log(str);

    【讨论】:

      【解决方案4】:

      const locs = [
          {
            "location":{
                "city":"New place 1",
                "other":"random"
            }
          },
          {
            "location":{
                "city":"New place 2",
                "other":"random dom"
            }
          }
      ]
      
      const res = locs.map(d => d.location.city).join(', ');
      console.log(res); // "New place 1, New place 2"

      locs.map(d => d.location.city).join(', ') 既美观又简单

      【讨论】:

        【解决方案5】:

        您可以使用.reducetemplate literals 来构建您的字符串。在这里,我还使用 .slice 删除了字符串末尾的逗号和空格:

        const locs = {locations:[{location:{city:"New place 1",other:"random"}},{location:{city:"New place 2",other:"random dom"}}]},
        
        res = locs.locations.reduce((acc, loc) => `${acc}${loc.location.city}, `, ``).slice(0,-2);
        console.log(res);

        如果您只想要一个数组作为输出,您可以使用.map:

         const locs = {locations:[{location:{city:"New place 1",other:"random"}},{location:{city:"New place 2",other:"random dom"}}]},
         
        arr = locs.locations.map(loc => loc.location.city);
        console.log(arr);

        【讨论】:

          【解决方案6】:

          一个使用mapString constructor的衬垫

          var a = {
            locations: [{
              location: {
                city: "New place 1",
                other: "random"
              }
            }, {
              location: {
                city: "New place 2",
                other: "random dom"
              }
            }]
          }
          console.log(String(a.locations.map(e => e.location.city)))

          【讨论】:

            猜你喜欢
            • 2022-11-23
            • 1970-01-01
            • 2021-12-06
            • 1970-01-01
            • 2015-03-19
            • 1970-01-01
            • 2021-02-21
            • 1970-01-01
            • 2023-01-16
            相关资源
            最近更新 更多