javascript - 将 Javascript 对象格式化为新数组

标签 javascript arrays javascript-objects

我在按照我需要的方式格式化数据时遇到了一些问题。目前,我有一个空函数

populateAddresses(data) {
    console.log(JSON.stringify(data))
}

这将输出采用以下格式的完整数据

{
  "county": "WEST MIDLANDS",
  "town": "SOLIHULL",
  "streets": [
    {
      "name": "MARSLAND ROAD",
      "numbers": [
        {
          "buildingNumberOrName": "1",
          "buildingName": "",
          "subBuildingName": "",
          "buildingNumber": "1"
        },
        {
          "buildingNumberOrName": "3",
          "buildingName": "",
          "subBuildingName": "",
          "buildingNumber": "3"
        }
      ]
    }
  ],
  "postcode": "B92 7BU"
}

所以它基本上是邮政编码的地址。我想要做的是创建一个完整地址数组。所以对于上面的例子,我的数组应该是这样的

["1 MARSLAND ROAD, SOLIHULL, WEST MIDLANDS, B92 7BU", "3 MARSLAND ROAD, SOLIHULL, WEST MIDLANDS, B92 7BU"]

因此它将是 buildingNumberOrName + 街道名称,然后是城镇、县和邮政编码。可能有不止一条街道,这可能会使事情进一步复杂化?

我在想也许循环对象来获取信息,比如

Object.keys(data).forEach(key => {

});

但我认为我将不得不使用许多内部循环。我看过映射,但不确定如何在此处使用它?

感谢任何建议。

谢谢

最佳答案

对此有嵌套循环不是问题。您的数据实际上只有两个级别(街道和数字),因此嵌套循环可以清楚而轻松地实现这一目的:

const array = [];
const {county, town, postcode} = data;
for (const {name, numbers} of data.streets) {
    for (const {buildingNumberOrName} of numbers) {
        array.push(`${buildingNumberOrName} ${name} ${town}, ${county}, ${postcode}`);
    }
}

实例:

const data = {
  "county": "WEST MIDLANDS",
  "town": "SOLIHULL",
  "streets": [
    {
      "name": "MARSLAND ROAD",
      "numbers": [
        {
          "buildingNumberOrName": "1",
          "buildingName": "",
          "subBuildingName": "",
          "buildingNumber": "1"
        },
        {
          "buildingNumberOrName": "3",
          "buildingName": "",
          "subBuildingName": "",
          "buildingNumber": "3"
        }
      ]
    }
  ],
  "postcode": "B92 7BU"
};

const array = [];
const {county, town, postcode} = data;
for (const {name, numbers} of data.streets) {
    for (const {buildingNumberOrName} of numbers) {
        array.push(`${buildingNumberOrName} ${name} ${town}, ${county}, ${postcode}`);
    }
}

console.log(array);

假设 streets 中后续条目的对象结构与您显示的相同,如果有多条街道,这就很好用。

或者如果您需要坚持使用 ES5 或更早版本:

var array = [];
var county = data.county, town = data.town, postcode = data.postcode;
data.streets.forEach(function(street) {
    var name = street.name;
    street.numbers.forEach(function(num) {
        array.push(num.buildingNumberOrName + " " + name + " " + town + ", " + county + ", " + postcode);
    });
});

实例:

var data = {
  "county": "WEST MIDLANDS",
  "town": "SOLIHULL",
  "streets": [
    {
      "name": "MARSLAND ROAD",
      "numbers": [
        {
          "buildingNumberOrName": "1",
          "buildingName": "",
          "subBuildingName": "",
          "buildingNumber": "1"
        },
        {
          "buildingNumberOrName": "3",
          "buildingName": "",
          "subBuildingName": "",
          "buildingNumber": "3"
        }
      ]
    }
  ],
  "postcode": "B92 7BU"
};

var array = [];
var county = data.county, town = data.town, postcode = data.postcode;
data.streets.forEach(function(street) {
    var name = street.name;
    street.numbers.forEach(function(num) {
        array.push(num.buildingNumberOrName + " " + name + " " + town + ", " + county + ", " + postcode);
    });
});

console.log(array);


这遵循您关于输出的问题的规则:

...buildingNumberOrName + streets name, then town, county and postcode...

但您可以在创建字符串时根据需要混合 numbers 数组中对象的其他属性。

关于javascript - 将 Javascript 对象格式化为新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58935084/

相关文章:

javascript - Ajax 更新和表单输入选择字段

javascript - JS/jQuery 中 'pacing' HTTP 请求的首选技术?

javascript - 有没有办法在javascript中复制对象数组?

javascript - 使用 Django 将对象从模板传递到 View

javascript - 映射默认值

javascript - 使用ExternalInterface获取swf文件函数名称?

python - 用numpy计算距离矩阵

c - 如何在c中找到char数组的长度

C - 在 while 循环中直接写入数组,同时使用 recv 接收数据

javascript - 在javascript中的backbone js中使用时如何创建模型对象