创建新的城邦对象

我想将初始数据转换为所需的结果,我正在努力将城市推送到数组并确保名称键是唯一的。


初始数据


[

  { "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"

    ]

  }

]

任何帮助/提示或指出解决此问题的正确方向将不胜感激。


慕哥6287543
浏览 57回答 2
2回答

翻阅古今

您基本上是按州对城市进行分组。首先,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;

POPMUISE

这是另一种考虑方法: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;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript