为了账号安全,请及时绑定邮箱和手机立即绑定

创建新的城邦对象

创建新的城邦对象

慕哥6287543 2023-09-28 15:22:34
我想将初始数据转换为所需的结果,我正在努力将城市推送到数组并确保名称键是唯一的。初始数据[  { "city": "Abbeville", "state": "Louisiana" },  { "city": "Aberdeen", "state": "Maryland" },  { "city": "Aberdeen", "state": "Mississippi" },  { "city": "Aberdeen", "state": "South Dakota" },  { "city": "Aberdeen", "state": "Washington" },  { "city": "Abilene", "state": "Texas" },  { "city": "Abilene", "state": "Kansas" },  { "city": "Abingdon", "state": "Virginia" },  { "city": "Abington", "state": "Massachusetts" },  { "city": "Abington", "state": "Massachusetts" },]代码 let newCityStateObject = cities.map((item, index) => {        console.log("item ", item);  if (item) {    let object = {};    let citiesArray = [];    //set state and create empty array    if (object[item.state] === undefined) {      object.name = item.state;      object.cities = [].push(item.city);    } else {      //key exists so just push to array      if (object[item.state]) {        object.cities.push(item.city);      }    }    console.log("end ", object);    return object;  }    });我现在的结果[  { state: 'Louisiana', cities: [] },  { state: 'Maryland', cities: [] },  { state: 'Mississippi', cities: [] },  { state: 'South Dakota', cities: [] },  { state: 'Washington', cities: [] },  { state: 'Texas', cities: [] },  { state: 'Kansas', cities: [] },  { state: 'Virginia', cities: [] },]期望的结果[{    "name": "Kentucky",    "cities": [      "Louisville/Jefferson County",      "Lexington-Fayette",      "Bowling Green",      "Owensboro",      "Covington"    ]  },  {    "name": "Maryland",    "cities": [      "Baltimore",      "Frederick",      "Rockville",      "Gaithersburg",      "Bowie",      "Hagerstown",      "Annapolis"    ]  }]任何帮助/提示或指出解决此问题的正确方向将不胜感激。
查看完整描述

2 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

您基本上是按州对城市进行分组。首先,array.map这不是解决此问题的正确方法,因为当您分组时,输入项的编号可能与输出项的编号不匹配。array.reduce是更好的选择。


let newCityStateObject = cities.reduce((acc, item, index) => {

  if (item) {

    // this object has state as key, and the desired output array’s item as value

    const object = acc.obj;


    // if state not found, create new record

    if (object[item.state] === undefined) {

      const record = { name: item.state, cities: [] }

      object[item.state] = record;

      acc.array.push(record);

    }


    const record = object[item.state];

    record.cities.push(item.city);

  }

  return acc;

}, { obj: {}, array: [] }).array;


查看完整回答
反对 回复 2023-09-28
?
POPMUISE

TA贡献1765条经验 获得超5个赞

这是另一种考虑方法:


let newCityStateObject = () => {

    const statesArr = Array.from(cities, (item) => item.state);

    // Remove state duplications

    const filteredStates = [...new Set(statesArr)];

    // Initializing the name and cities object and array

    let result = filteredStates.map((item) => {

      return { name: item, cities: [] };

    });


    // For each cite item, fetch the city to its corresponding result by state

    cities.forEach((item) =>

      result

        .find((element) => element.name === item.state)

        .cities.push(item.city)

    );

    return result;

}


查看完整回答
反对 回复 2023-09-28
  • 2 回答
  • 0 关注
  • 55 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信