亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建新的城邦對象

創建新的城邦對象

慕哥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 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號