当数组中的相似元素与另一个数组中的值匹配时,Javascript 将其从数组中删除,但仅限于第二个数组中的元素数量

问题描述 投票:0回答:1

当一个数组与另一个数组中的值匹配时,从数组中删除相似元素,但仅限于第二个数组中的元素数量。

我有2个数组:

var items = [{quantity: 3, name: "test Item 1", relatedId: "hill123", Id: "111-111"}, 
            {quantity: 1, name: "test Item 2", relatedId: "lane222", Id: "222-222"},
            {quantity: 1, name: "test Item 3", relatedId: "buncha333", Id: "333-333"}
            
            ];
var relatedItems =[{quantity: 1, name: "Related Item 1", Id: "hill123"},
                    {quantity: 1, name: "Related Item 1", Id: "hill123"},
                    {quantity: 1, name: "Related Item 2", Id: "bump222"},
                    {quantity: 1, name: "Related Item 3", Id: "buncha333"}
];

我需要 1 个新数组,其中包含尚不存在且匹配、删除的相关项目。

例如,“测试项目 1”有 3 个数量,但 relatedItems 列表中只有 2 个相关项目,因此我需要在新数组中添加 1 个项目。加上没有匹配的相关项目的“测试项目 2”,但不是“测试项目 3”,因为它有一个相关项目,并且 1 个数量与 1 个相关项目匹配。

所以最后我需要一个如下所示的数组:

filterList = [{Id: "hill123"}, {Id: "lane222"}]

我已经尝试了很多东西,但这是我最后的几次尝试,我真的认为最上面的一个会起作用 - 基于类似的问题,但它不断删除太多元素,只留下“单个项目:lane222”而不是需要 2 个。

var items = [{quantity: 3, name: "test Item 1", relatedId: "hill123", Id: "111-111"}, 
            {quantity: 1, name: "test Item 2", relatedId: "lane222", Id: "222-222"},
            {quantity: 1, name: "test Item 3", relatedId: "buncha333", Id: "333-333"}
            
            ];
var relatedItems =[{quantity: 1, name: "Related Item 1", Id: "hill123"},
                    {quantity: 1, name: "Related Item 1", Id: "hill123"},
                    {quantity: 1, name: "Related Item 2", Id: "bump222"},
                    {quantity: 1, name: "Related Item 3", Id: "buncha333"}
];

var singleQuantity = [];
items.forEach(function(item){
  //console.log('item: ' + item.name + " --> " + item.quantity + " --> " + item.relatedId)
  if(item.quantity > 1){
    for(var i=0; i < item.quantity; i++){
      singleQuantity.push(item);
    } 
  }else {
      singleQuantity.push(item);
    }
});

if(singleQuantity){
  singleQuantity.forEach(function(item){
    item.quantity = 1;
  })
}

var relatedIdsOnly = [];
var itemRelatedIdsOnly = [];

singleQuantity.forEach(function(item){
  itemRelatedIdsOnly.push(item.relatedId);
})

relatedItems.forEach(function(item){
  relatedIdsOnly.push(item.Id)
})

for (var i = 0; i< relatedIdsOnly.length; i++) {
    var arrlen = itemRelatedIdsOnly.length;
    for (var j = 0; j<arrlen; j++) {
        if (relatedIdsOnly[i] == itemRelatedIdsOnly[j]) {
            itemRelatedIdsOnly = itemRelatedIdsOnly.slice(0, j).concat(itemRelatedIdsOnly.slice(j+1, arrlen));
        }
    }
    // gives me only 1 item, not 2.  "Single item: lane222" it should have 1 item: hill123 also
}

itemRelatedIdsOnly.forEach(function(item){
  console.log('Single item: ' + item)
});



// for( var i = 0; i < relatedItems.length; i++){
//   var arrlen = singleQuantity.length;
//   console.log(arrlen + " " + relatedItems.length)
//   for (var j = 0; j < arrlen; j++){
//     if (relatedItems[i].Id == singleQuantity[j].relatedId){
//       console.log('match ' + relatedItems[i].Id + " " + singleQuantity[j].relatedId)
//       singleQuantity = singleQuantity.slice(0, j).concat(singleQuantity.slice(j+1, arrlen))
//       // did not remove anything - gave me an error - TypeError: Cannot read properties of undefined (reading 'relatedId')
//       // at <anonymous>:76:49
//       // at mn (<anonymous>:16:5455)
//     }
//   }
// }


// for( var i = 0; i < relatedItems.length; i++){
//   var arrlen = singleQuantity.length;
//   for (var j = 0; j < arrlen; j++){
//     if (relatedItems[i].Id == singleQuantity[j].relatedId){
//       console.log('match ' + relatedItems[i].Id + " " + singleQuantity[j].relatedId)
//       singleQuantity = singleQuantity.slice(0, j).concat(singleQuantity.slice(j+1, arrlen))
//       // gave me an error -
//     }
//   }
// }


// relatedItems.forEach(function(rel){
//   console.log('rel: ' + rel.Id)
//   singleQuantity.forEach(function(item, index, object ){
//     console.log(item.name)
//     if(item.relatedId === rel.Id){
//       object.splice(index, 1)
        
//     }
//   });
// });

// singleQuantity.forEach(function(rel, index, object ){
//   relatedItems.forEach(function(item){
//     if(item.relatedId === rel.Id){
//       object.splice(index, 1) // didnt splice anything        
//     }
//   });
// })



// relatedItems.forEach(function(rel){
//   //console.log('rel: ' + rel.Id)
//   items.forEach(function(item, index, object ){
//     //console.log(item.name)
//     if(item.relatedId === rel.Id && item.quantity > 1){
//       dupsList.push(item);
//     } else if(item.relatedId !== rel.Id && item.quantity === 1){
//       //console.log('not match')
//       filteredList.push(rel.Id);
//     }
//   });
// });


javascript arrays filter splice
1个回答
0
投票

类似这样的事情?

const
  items = 
    [ { quantity: 3, name: 'test Item 1', relatedId: 'hill123',   Id: '111-111' } 
    , { quantity: 1, name: 'test Item 2', relatedId: 'lane222',   Id: '222-222' } 
    , { quantity: 1, name: 'test Item 3', relatedId: 'buncha333', Id: '333-333' } 
    ] 
, relatedItems = 
    [ { quantity: 1, name: 'Related Item 1', Id: 'hill123'   } 
    , { quantity: 1, name: 'Related Item 1', Id: 'hill123'   } 
    , { quantity: 1, name: 'Related Item 2', Id: 'bump222'   } 
    , { quantity: 1, name: 'Related Item 3', Id: 'buncha333' } 
    ]
, result = []
  ;

for (let {quantity:Qte, relatedId:Id} of items) 
  {
  let Qsum = relatedItems.reduce((sum,{quantity,Id:rId}) => sum += Id===rId ? quantity : 0, 0);
  if (Qsum > 0) result.push({Id});
  }

document.write( JSON.stringify(result))

© www.soinside.com 2019 - 2024. All rights reserved.