'Return Boolean if element of first array is in element of second nested array
I have two different types of arrays
firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}].
The second object looks like this secondObject = {someAttribute: bla, numbers: [23, 26, 27, 28}
Now i want to check if at least one of the provided number fields of the firstArrayObject is not part of the secondObject's numbers Array. If it is not part, stop the search progress and return true. In the provided example it should return true, because "25" is not part of secondObject.numbers
Im struggling with all the different methods es6 provides like includes, filter, some.
Hopefully anyone can help me out here.
Solution 1:[1]
Hope this helps!! Used forEach and includes function of array. A good resource to learn javascript array functions.
var answer = false;
const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];
const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};
function func(obj) {
if(!secondObject.numbers.includes(obj.number)) answer = true;
}
firstArrayObject.forEach(func);
console.log(answer)
Edit: func can be created as the forEach parameter itself, although the above implementation might make it more readable for beginners.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
