Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're on the right track with some:

const result = firstArrayObject.some(
    ({number}) => !secondObject.numbers.includes(number)
);

const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];
const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

const result = firstArrayObject.some(({number}) => !secondObject.numbers.includes(number));

console.log(result);

That says: "Do some (any) of firstArrayObject's elements match the condition 'number is not included in the array `secondObject.numbers'?"

I used destructuring there, but you don't have to:

const result = firstArrayObject.some(
    (element) => !secondObject.numbers.includes(element.number)
);

If secondObject.numbers were a very larger array, you might start out by making a Set of the known numbers, because the has function of a Set is required (by the JavaScript specification) to have better performance than the linear lookup time of includes:

const numbers = new Set(secondObject.numbers);
const result = firstArrayObject.some(
    ({number}) => !numbers.has(number)
);

const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];
const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

const numbers = new Set(secondObject.numbers);
const result = firstArrayObject.some(
    ({number}) => !numbers.has(number)
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!