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?
-
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
3 Answers
Reset to default 3Using 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);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745474124a4629267.html
评论列表(0条)