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

时间:2016-11-17 01:25:47

标签: javascript arrays object underscore.js

我有一个名为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字段等于false的obj1中是否存在arrray obj1.Active的元素。有什么方法可以将这个变量添加到我的代码中,我可以检查这个条件吗?

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

使用下划线,使用_.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>
&#13;
&#13;
&#13;

更通用的方法是使用_.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;
});

&#13;
&#13;
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);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;

您甚至可以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" });

答案 2 :(得分:1)

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

&#13;
&#13;
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);
&#13;
&#13;
&#13;