检查对象数组中是否存在变量的条件

Condition to check if variables exist in an array of objects

我有一个名为 data 的数组,如下所示,还有一个名为 obj1 的对象数组。

var data = ["004", "456", "333", "555"];


obj1 = [{
  id: "004",
  name: "Rick",
  Active: "false"
  }, {
    id: "005",
    name: 'david',
    Active: "false"
}];

我检查数组 data 的元素是否存在于 obj1 中。这是它的代码。它将给出 "004" 作为答案,因为 004 出现在 obj1.

out = [];

_.each(arr, function(value1, key1, obj1) {
   _.each(data, function(value, key, obj) {
        if (value1.id == value) out.push(value);
   });
});

console.log(out);

现在我也想添加以下内容

var Activecheck = false;

我想检查数组 data 的元素是否存在于 obj1 中,其中 obj1.Active 字段等于 false。有什么方法可以将此变量添加到我可以检查此条件的代码中?

使用 Array.filter 可能更干净,如下所示:

var activeCheck = "false";
var out = obj1.filter(function(item) {
  return item.active === activeCheck && data.includes(item.id);
});

带下划线,使用_.filter function:

var isActive = "false"; // get that value dynamically
var result = _.filter(obj1, function(value) {
  return data.indexOf(value.id) > -1 && value.Active === isActive;
});

var data = ["004", "456", "333", "555"];


obj1 = [{
  id: "004",
  name: "Rick",
  Active: "false"
}, {
  id: "005",
  name: 'david',
  Active: "false"
}];

var result = _.filter(obj1, function(value) {
  return data.indexOf(value.id) > -1 && value.Active === "false";
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

一种更通用的方法是使用 _.where 按任何属性进行过滤,在对象中轻松设置,您可以动态构建:

var filters = { Active: "false" }; // add any attributes to filter more precisely

var filtered = _.where(obj1, filters);

var result = _.filter(filtered, function(value) {
  return data.indexOf(value.id) > -1;
});

var data = ["004", "456", "333", "555"];


obj1 = [
  { id: "004", name: "Rick", Active: "false"}, 
  { id: "005", name: 'david', Active: "false"},
  { id: "456", name: "Steeve", Active: "false", test: 1}, 
];

var filters = { Active: "false", test: 1 };

var filtered = _.where(obj1, filters);

var result = _.filter(filtered, function(value) {
  return data.indexOf(value.id) > -1;
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

您甚至可以 chain 调用:

var result = _.chain(obj1)
    .where({ Active: "false" })
    .filter(function(value) {
        return data.indexOf(value.id) > -1;
    })
    .value();

作为函数:

function filterWithData(obj, filters) {
    // if you want to filter (e.g. Active) by default
    // filters = _.extend({ Active: "false" }, filters);
    return _.chain(obj)
        .where(filters)
        .filter(function(value) {
            return data.indexOf(value.id) > -1;
        })
        .value();
}

然后在需要时使用该功能:

var result = filterWithData(obj1, { Active: "false" });

您可以简单地使用过滤器和包含来实现您想要的。

var data = ["004", "456", "333", "555"];


var obj1 = [{
  id: "004",
  name: "Rick",
  Active: "false"
}, {
  id: "005",
  name: 'david',
  Active: "false"
}];

var result = obj1.filter((ele, idx) => {
  return data.includes(ele.id) && ele.Active === "false";
});

console.log(result);