How to check if array has more than one specific value by one iteration For example, we have two arrays:
const arr = [{prop: 'test'}, {prop: "test1"}, {prop: "test3"}];
const arr2 = [{prop: 'test3'}, {prop: "test"}, {prop: "test2"}];
I need to get true
if array object elements have prop
with value test
and test1
when I put it in if
statement for example.I need to get true
only if both values exists, If we have not at least one of them I need to get false
:
const arr = [{prop: 'test'}, {prop: "test1"}, {prop: "test3"}]; //true
const arr2 = [{prop: 'test3'}, {prop: "test"}, {prop: "test2"}]; //false
To do it in a single pass, build a structure that keeps track of the prop values that must be present (must not be missing). Run through the array and tick them off as they are found. This runs in O(n) where n is the length of the big array...
// array is a long array of objects // prop is the prop to check in the long array // mustHaves is a short array of values that must be present in prop function check(array, prop, mustHaves) { let missing = {} for (const key of mustHaves) missing[key] = true array.forEach(e => { if (missing[e[prop]]) { missing[e[prop]] = false } }) return Object.values(missing).every(v => !v) } const arr = [{prop: 'test'}, {prop: "test1"}, {prop: "test3"}]; const arr2 = [{prop: 'test3'}, {prop: "test"}, {prop: "test2"}]; const propToCheck = 'prop' const mustHaves = ['test', 'test1'] console.log(check(arr, propToCheck, mustHaves)) console.log(check(arr2, propToCheck, mustHaves))
You can use Array.prototype.every()
to check a pre-existing whitelist (ie test
and test1
) against your array. You can conver your array of objects objects into plain array that contains just the prop
values for easy comparison, using Array.prototype.map()
:
const mappedArray = arr.map(v => v.prop);
And then, you can simply check if all elements in your whitelist is found in this mapped array:
['test', 'test1'].every(x => mappedArray.includes(x));
See proof-of-concept below:
const arr = [{prop: 'test'}, {prop: "test1"}, {prop: "test3"}]; const arr2 = [{prop: 'test3'}, {prop: "test"}, {prop: "test2"}]; function mustContainValues(arr, whitelist) { const mappedArr = arr.map(v => v.prop); return whitelist.every(x => mappedArr.includes(x)); } console.log(mustContainValues(arr, ['test', 'test1'])); // true console.log(mustContainValues(arr2, ['test', 'test1'])); // false
You can try to build an object using reduce:
const func = (arr, value1, value2) =>
{
let obj = arr.reduce((acc, rec) => {
if(rec.prop === value1) acc['hasValue1'] = true
if(rec.prop === value2) acc['hasValue2'] = true
return acc
}, {hasValue1: false, hasValue2: false})
return obj['hasValue1'] && obj['hasValue2']
}
It will build an object like {hasValue1: false, hasValue2: false}
and then return boolean AND for found flags. Reduce function will iterate through an array only once Invoke this function using:
console.log(func(arr, 'test', 'test1'))
console.log(func(arr2, 'test', 'test1'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.