【问题标题】:Filter array of objects with another array of objects and add extra key to matching objects用另一个对象数组过滤对象数组,并为匹配的对象添加额外的键
【发布时间】:2019-05-05 17:35:01
【问题描述】:

我有以下两个数组,我想获得第二个数组,但是匹配的对象应该包含一个额外的键 "select" : true, 而不匹配的对象应该包含 "select" :错误。

var firstArray = [{id: -1, type: "group"},
                  {id: -2, type: "group"}];

var secondArray = [{id: -3, type: "group"},
                   {id: -2, type: "group"},
                   {id: -1, type: "group"}];

预期结果:

var expectedArray = [{id: -1, type: "group", select: true},
                     {id: -2, type: "group", select: true},
                     {id: -3, type: "group", select: false}];

我试过下面的代码:

for(var i=0;i<firstArray.length;i++){
             for(var j=0;j<secondArray.length;j++){
               console.log(firstArray[i].id,secondArray[j].id)
               if(firstArray[i].id===secondArray[j].id){
                 if(secondArray[j].hasOwnProperty('select')){
                   secondArray[j].select=true;
                   // console.log('true select property',secondArray[j].select)
                 }
                 else{
                   secondArray[j].select=true;
                   // console.log('true adding new',secondArray[j].select)
                 }
               }else{
                  secondArray[j]['select']=false;
                 // console.log('false not match',secondArray[j].select)
               }
             }
          }

【问题讨论】:

  • @user11299053,当然,它有效,请为我的问题投票。

标签: javascript arrays


【解决方案1】:

您可以为firstArray 中存在的所有ID 创建一个Set。然后使用forEach循环遍历secondArray并根据是否设置has的id添加select

let firstArray=[{id:-1,type:"group"},{id:-2,type:"group"}],
    secondArray=[{id:-3,type:"group"},{id:-2,type:"group"},{id:-1,type:"group"}];

const ids = new Set(firstArray.map(a => a.id));
secondArray.forEach(a => a.select = ids.has(a.id))

console.log(secondArray)

【讨论】:

    【解决方案2】:

    这应该可行:

    var ans = secondArray.map(s=> {
    var match = firstArray.find(f=> f.id === s.id);
    if(match) s['select'] = true;
    else s['select'] = false;
    return s;
    })
    

    【讨论】:

      【解决方案3】:

      与其修改secondArray,不如不直接修改。我们可以通过创建另一个具有select 属性的数组来使用不可变的方式。对于这种情况,我们将使用map

      var firstArray = [{id: -1, type: "group"},
                        {id: -2, type: "group"}];
      
      var secondArray = [{id: -3, type: "group"},
                         {id: -2, type: "group"},
                         {id: -1, type: "group"}];
      
      const expectedArray = secondArray.map(item => {
        const idExist = firstArray.find(itemInFirst => itemInFirst.id === item.id);
        return {
          ...item,
          select: idExist !== undefined
        }
      });
      
      console.log(expectedArray);

      【讨论】:

        【解决方案4】:

        只需一行代码即可:

        var firstArray = [
        	{id: -1, type: "group"}, 
        	{id: -2, type: "group"}
        ];
        
        var secondArray = [
        	{id: -3, type: "group"}, 
        	{id: -2, type: "group"}, 
        	{id: -1, type: "group"}
        ];
        
        const result = secondArray.map(item => ({...item, 'select': firstArray.some(entry => entry.id == item.id)}));
        
        console.log(result);
        .as-console-wrapper {
          max-height: 100% !important;
          color: white;
          top: 0;
        }
        
        .as-console-row {
          background: black;
        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-03
          • 1970-01-01
          • 2021-05-14
          • 2021-10-16
          • 2019-05-05
          相关资源
          最近更新 更多