【问题标题】:Merge Arrays of Objects, Modifying Some Resulting Entries合并对象数组,修改一些结果条目
【发布时间】:2019-06-09 04:36:00
【问题描述】:

我有以下代码,它在 JavaScript 中获取 2 个对象数组并比较它们并合并内容,返回第一个数组中的所有对象,以及第二个数组中的所有对象,其 id 不在第一个数组中。

但是对于来自第二个数组的那些项目,我想修改它在新数组中的条目以仅包含某些属性(例如,id & make)并添加一个新的占位符属性(例如,输入:“汽车”)。我相信这会使用地图功能,但我似乎无法让它工作。

    var cars1 = [
        {id: 1, make: "Ford",  model: "F150",  year: 2002},
        {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
    ];

    var cars2 = [
        {id: 2, make: "Kia",    model: "Optima",  year: 2001},
        {id: 4, make: "Nissan", model: "Sentra",  year: 1982},
    ];

    const cars1IDs = new Set(cars1.map(({ id }) => id));
    const combined = [
      ...cars1,
      ...cars2.filter(({ id }) => !cars1IDs.has(id))
              .map(cars2 => cars2.id)
    ];
    combined.sort(({ id: aId }, {id: bId }) => aId - bId);
    console.log(combined);

我希望组合数组以以下内容结束:

var combined = [
    {id: 1, make: "Ford",   model: "F150",  year: 2002},
    {id: 2, make: "Kia",    type: "car"},
    {id: 3, make: "Chevy",  model: "Tahoe", year: 2003},
    {id: 4, make: "Nissan", type: "car"},
];

但结果却是:

var combined = [
    {id: 1, make: "Ford",   model: "F150",  year: 2002},
    {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
    2,
    4
];

【问题讨论】:

    标签: javascript dictionary filter


    【解决方案1】:

    在.map 中,您应该提取id 和make 属性,并返回一个仅包含这些属性以及type 的car 的对象:

    var cars1 = [
        {id: 1, make: "Ford",  model: "F150",  year: 2002},
        {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
    ];
    
    var cars2 = [
        {id: 2, make: "Kia",    model: "Optima",  year: 2001},
        {id: 4, make: "Nissan", model: "Sentra",  year: 1982},
    ];
    
    const cars1IDs = new Set(cars1.map(({ id }) => id));
    const combined = [
      ...cars1,
      ...cars2.filter(({ id }) => !cars1IDs.has(id))
              .map(({ id, make }) => ({ id, make, type: 'car' }))
    ];
    combined.sort((a, b) => a.id - b.id);
    console.log(combined);

    【讨论】:

    • 这是完美的。谢谢!
    【解决方案2】:

    你应该改变地图功能 这是代码。只有car2的地图功能是变化的

    var cars1 = [
            { id: 1, make: "Ford", model: "F150", year: 2002 },
            { id: 3, make: "Chevy", model: "Tahoe", year: 2003 }
          ];
    
          var cars2 = [
            { id: 2, make: "Kia", model: "Optima", year: 2001 },
            { id: 4, make: "Nissan", model: "Sentra", year: 1982 }
          ];
    
          const cars1IDs = new Set(cars1.map(({ id }) => id));
          const combined = [
            ...cars1,
            ...cars2
              .filter(({ id }) => !cars1IDs.has(id))
              .map(car2 => {
                  //add new prop type to car
                  const tempCar2 = Object.assign(car2,{type:"car"});
                  //remove year and model
                  delete tempCar2.year                    
                  delete tempCar2.model
                  return tempCar2;
                //Object.assign(car2, { id: car2.id });
              })
          ];
          combined.sort(({ id: aId }, { id: bId }) => aId - bId);
          console.log(combined);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多