如何基于2个键获取不同的数组列表?

时间:2019-01-13 14:30:25

标签: javascript arrays

我有以下2个数组。

const array1 = [
    {name: "V1", year: "2018", isDefault: false},
    {name: "V2", year: "2018", isDefault: false},
    {name: "V3", year: "2018", isDefault: false},
    {name: "V4", year: "2018", isDefault: false},
    {name: "V1", year: "2019", isDefault: true}
  ];

const array2 = [
    {name: "V1", year: "2018", isDefault: false},
    {name: "V2", year: "2018", isDefault: false},
    {name: "V3", year: "2018", isDefault: false},
    {name: "V4", year: "2018", isDefault: false},
    {name: "V1", year: "2020", isDefault: true}
  ];

我想得到一个结果数组,其中包含array1和array2中的所有项目,但是基于名称和年份(基于2个键的组合)仅具有一次公共项目。我必须得到如下结果数组:

const array = [
    {name: "V1", year: "2018", isDefault: false},
    {name: "V2", year: "2018", isDefault: false},
    {name: "V3", year: "2018", isDefault: false},
    {name: "V4", year: "2018", isDefault: false},
    {name: "V1", year: "2019", isDefault: true},
    {name: "V1", year: "2020", isDefault: true}
  ];

请帮助我如何使用javascript?

4 个答案:

答案 0 :(得分:3)

您可以通过将array2的过滤版本连接到array1来实现。

const array1 = [
    {name: "V1", year: "2018", isDefault: false},
    {name: "V2", year: "2018", isDefault: false},
    {name: "V3", year: "2018", isDefault: false},
    {name: "V4", year: "2018", isDefault: false},
    {name: "V1", year: "2019", isDefault: true}
];

const array2 = [
    {name: "V1", year: "2018", isDefault: false},
    {name: "V2", year: "2018", isDefault: false},
    {name: "V3", year: "2018", isDefault: false},
    {name: "V4", year: "2018", isDefault: false},
    {name: "V1", year: "2020", isDefault: true}
];

let res = array1.concat(
    array2.filter(({name, year}) => !array1.some(y => y.name === name && y.year === year))
);

console.log(res);

答案 1 :(得分:1)

使用Array.prototype.filterSet的简单快捷的解决方案:

const array1 = [{name: "V1", year: "2018", isDefault: false},{name: "V2", year: "2018", isDefault: false},{name: "V3", year: "2018", isDefault: false},{name: "V4", year: "2018", isDefault: false},{name: "V1", year: "2019", isDefault: true}];

const array2 = [{name: "V1", year: "2018", isDefault: false},{name: "V2", year: "2018", isDefault: false},{name: "V3", year: "2018", isDefault: false},{name: "V4", year: "2018", isDefault: false},{name: "V1", year: "2020", isDefault: true}];

const array = [...array1, ...array2].filter(function({name, year}) {

    const key = `${name}${year}`;

    return !this.has(key) && this.add(key);

}, new Set);


console.log(array);

答案 2 :(得分:0)

const array1 = [{
    name: "V1",
    year: "2018",
    isDefault: false
  },
  {
    name: "V2",
    year: "2018",
    isDefault: false
  },
  {
    name: "V3",
    year: "2018",
    isDefault: false
  },
  {
    name: "V4",
    year: "2018",
    isDefault: false
  },
  {
    name: "V1",
    year: "2019",
    isDefault: true
  }
];

const array2 = [{
    name: "V1",
    year: "2018",
    isDefault: false
  },
  {
    name: "V2",
    year: "2018",
    isDefault: false
  },
  {
    name: "V3",
    year: "2018",
    isDefault: false
  },
  {
    name: "V4",
    year: "2018",
    isDefault: false
  },
  {
    name: "V1",
    year: "2020",
    isDefault: true
  }
];

for (let i = 0; i < array1.length; i++) {
  for (let j = 0; j < array2.length; j++) {
    if (array1[i].name == array2[j].name && array1[i].year == array2[j].year) {
      array2.splice(j, 1)
    }
  }
}

const array = array1.concat(array2);
console.log(array);

我试图解决您的问题,可能会为您提供帮助

答案 3 :(得分:0)

您也可以尝试以下

const mergeById =(array1,array2)=>     array1.map(itm =>   ({... array2.find((item)=>(item.name === itm.name)&&(item.year === itm.year)&& item),... itm})); < / p>