【问题标题】:create new city state object创建新的城市状态对象
【发布时间】:2021-03-13 22:31:08
【问题描述】:

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

初始数据

[
  { "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"
    ]
  }
]

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

【问题讨论】:

  • [].push(item.city); [item.city]吧。
  • @Taplar 这样做不会推送到数组,它只会将值设置为字符串。我需要城市成为一个数组,我可以继续添加它
  • jsfiddle.net/8b5fo029 它没有将其设置为字符串。如果您创建的数组已经包含该元素,则不必将其推送到数组中。

标签: javascript node.js arrays sorting


【解决方案1】:

我喜欢@hackape 提出的答案。

这是另一种考虑的方法:

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;
}

【讨论】:

    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 2016-12-07
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多