【问题标题】:How to Remove Specific object from arrayList using Javascript or Lodash如何使用 Javascript 或 Lodash 从 arrayList 中删除特定对象
【发布时间】:2016-08-16 06:19:13
【问题描述】:

我有两个这样的对象。

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'
    }];

我想删除与 find 对象的所有三个属性匹配的 Result 对象。我希望结果应该是这样的:

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


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

你能帮忙吗?

【问题讨论】:

  • 请发表你的努力
  • 你确定这就是你想要的最终结果吗?我的意思是您获取 licenseId 并将其作为 batchId。最终结果是您的 licenseId 为 3456,但它甚至不存在于起始数组中。

标签: javascript node.js underscore.js lodash


【解决方案1】:

您可以使用数组find 方法来查找结果数组是否有匹配的元素。这里licenseId用于查找结果数组是否包含相同的元素。

如果找到它,请使用index 参数来查找它的索引。然后使用splice 删除特定元素。

您也可以使用数组forEach 循环遍历find 数组

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

【讨论】:

    【解决方案2】:

    以下代码应该可以工作。

    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;
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      我会使用removesome

      _.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);
      &lt;script src="https://cdn.jsdelivr.net/lodash/4.15.0/lodash.min.js"&gt;&lt;/script&gt;

      【讨论】:

        猜你喜欢
        • 2019-07-11
        • 1970-01-01
        • 2012-01-21
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多