简体   繁体   中英

How to Remove Specific object from arrayList using Javascript or Lodash

I have two objects like this.

var find = [{
    licenseId: 'A123',
    batchId: '123',
    name: 'xxx'
},
    {
        licenseId: 'B123',
        batchId: '124',
        name: 'yyy'
    }];


var result = [
    {
        licenseId: 'A123',
        batchId: '123',
        name: 'xxx',
        tag: 'college',
        sem: 'fourth'
    },

    {
        licenseId: 'B123',
        batchId: '124',
        name: 'yyy',
        tag: 'college',
        sem: 'third'
    },
    {
        licenseId: '1111',
        batchId: 'C123',
        name: 'yyy',
        tag: 'college',
        sem: 'second'
    },


    {
        licenseId: '3456',
        batchId: 'B123',
        name: 'yyy',
        tag: 'college',
        sem: 'second'
    }];

I want to remove the objects of Result which has matched with all three properties of find object. I want the result should be like this:

[{
        licenseId: '1111',
        batchId: 'C123',
        name: 'yyy',
        tag: 'college',
        sem: 'second'
    },


    {
        licenseId: '3456',
        batchId: 'B123',
        name: 'yyy',
        tag: 'college',
        sem: 'second'
    }];

Can you assist?

Following code should work.

for(var j=0;j < find.length;j++){
  for (var i = 0; i < result.length; i++) {
    if ((result[i].licenseId == find[j].licenseId)  && 
        (result[i].name == find[j].name) && 
        (result[j].batchId == find[j].batchId)) {
      result.splice(i, 1);
      break;
    }
  }
}

You can use array find method to find if the result array has the matched element. Here licenseId is used to find if result array contain the same element.

If it is found there use index argument to find it's index. Then use splice to delete the specific element.

also you can use array forEach to loop through the find array

var find = [// json objects];

var result = [// json objects];
find.forEach(function(item){

var _findInResult = result.find(function(itemInResult,index){
   if(itemInResult.licenseId == item.licenseId){
   result.splice(index,1);
   }
   return itemInResult.licenseId == item.licenseId
})

})
console.log(result)

JSFIDDLE

I would use remove and some

_.remove(result, function(obj) {
    return _.some(find, {
        licenseId: obj.licenseId,
        batchId: obj.batchId,
        name: obj.name,
    });
});

 var find = [{ licenseId: 'A123', batchId: '123', name: 'xxx' }, { licenseId: 'B123', batchId: '124', name: 'yyy' }]; var result = [ { licenseId: 'A123', batchId: '123', name: 'xxx', tag: 'college', sem: 'fourth' }, { licenseId: 'B123', batchId: '124', name: 'yyy', tag: 'college', sem: 'third' }, { licenseId: '1111', batchId: 'C123', name: 'yyy', tag: 'college', sem: 'second' }, { licenseId: '3456', batchId: 'B123', name: 'yyy', tag: 'college', sem: 'second' }]; _.remove(result, function(obj) { return _.some(find, { licenseId: obj.licenseId, batchId: obj.batchId, name: obj.name, }); }); console.log(result); 
 <script src="https://cdn.jsdelivr.net/lodash/4.15.0/lodash.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM