最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Condition to check if variables exist in an array of objects - Stack Overflow

programmeradmin1浏览0评论

I have an array named data as shown below and an array of objects named obj1.

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


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

I check if elements of array data are present in the obj1. This is the code for that. It will give that "004" as the answer as 004 is present in the obj1.

out = [];

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

console.log(out);

Now i want the following to be added too

var Activecheck = false;

I want to check if elements of arrray data is present in obj1 where the obj1.Active field is equal to false. Any way to add this variable into my code where I can check this condition?

I have an array named data as shown below and an array of objects named obj1.

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


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

I check if elements of array data are present in the obj1. This is the code for that. It will give that "004" as the answer as 004 is present in the obj1.

out = [];

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

console.log(out);

Now i want the following to be added too

var Activecheck = false;

I want to check if elements of arrray data is present in obj1 where the obj1.Active field is equal to false. Any way to add this variable into my code where I can check this condition?

Share Improve this question edited Nov 17, 2016 at 1:52 Emile Bergeron 17.4k5 gold badges85 silver badges132 bronze badges asked Nov 17, 2016 at 1:25 Adu RaoAdu Rao 1012 gold badges4 silver badges10 bronze badges 4
  • 2 if (value1.id == value && value1.Active == false) ? – Aᴍɪʀ Commented Nov 17, 2016 at 1:30
  • 1 You can filter your obj1 array with condition active = false. It will give you only object with active false. Use Array.filter, and then you can use Array.some to check if value exists in filtered array. – Sandip Nirmal Commented Nov 17, 2016 at 1:31
  • @Amir- The value 'Activecheck' can be true or false and it is dynamic in nature. hence i cannot directly put a condition of false. – Adu Rao Commented Nov 17, 2016 at 1:33
  • if (value1.id == value && value1.Active == Activecheck) ? – Aᴍɪʀ Commented Nov 17, 2016 at 1:39
Add a ment  | 

3 Answers 3

Reset to default 3

Using Array.filter might be cleaner, like so:

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

With underscore, use the _.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./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

A more generic way is to use _.where to filter by any attributes, easily set in an object, which you can build dynamically:

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./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

You can even chain calls:

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

And as a function:

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

Then use the function whenever you need it:

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

You could simply use filter and includes to achieve what you want.

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

发布评论

评论列表(0)

  1. 暂无评论