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

223
Views
Check if an array includes some objects

As shown below, if I would like to find whether an object in testArray is in the currentArray, I can use for loop to traverse arrays, or I can use find(), some(), include(). The worst performance is if I go all fall into at least once O(n^2).

If I firstly filter the items in currentArray which the balance is not 999 or 999.999 and then to check testArray. Does the performance be better than fall into at least once? Or is there any way that doing better?

Thanks!

let currentArray = [
  {account: 1, balance: 200},
  {account: 2, balance: 100},
  {account: 3, balance: 300},
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 15, balance: 999.999},
  {account: 8, balance: 100},
  {account: 9, balance: 300},
  {account: 10, balance: 300},
]

let testArray = [
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 7, balance: 999.999},
  {account: 8, balance: 999},
  {account: 9, balance: 999.999},
  {account: 10, balance: 999},
  {account: 11, balance: 999.999},
  {account: 12, balance: 999},
  {account: 13, balance: 999.999},
  {account: 14, balance: 999},
  {account: 15, balance: 999.999},
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From what I understand, you expect the following result for the given example arrays:

[
    { "account":  4, "balance": 999     },
    { "account":  5, "balance": 999.999 },
    { "account":  6, "balance": 999     },
    { "account": 15, "balance": 999.999 }
]

...as these occur in both arrays.

To achieve that with a better time complexity, use a unique key for each object. Knowing that your objects have a key that consists of two properties, you can combine those two values in an array and produce its JSON encoding (or any other suitable encoding): that string will be a unique id for each object. Store those of the first array in a set, and then iterate the second array to check if the object has such an identifier that occurs in the set.

let currentArray = [
  {account: 1, balance: 200},
  {account: 2, balance: 100},
  {account: 3, balance: 300},
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 15, balance: 999.999},
  {account: 8, balance: 100},
  {account: 9, balance: 300},
  {account: 10, balance: 300},
];

let testArray = [
  {account: 4, balance: 999},
  {account: 5, balance: 999.999},
  {account: 6, balance: 999},
  {account: 7, balance: 999.999},
  {account: 8, balance: 999},
  {account: 9, balance: 999.999},
  {account: 10, balance: 999},
  {account: 11, balance: 999.999},
  {account: 12, balance: 999},
  {account: 13, balance: 999.999},
  {account: 14, balance: 999},
  {account: 15, balance: 999.999},
];

const hash = ({account, balance}) => JSON.stringify([account, balance]);

let ref = new Set(currentArray.map(hash));
let result = testArray.filter(obj => ref.has(hash(obj)));
console.log(result);

If you are only interested whether there is such an object, then use some instead of filter. The return value will be a boolean: true when there is such an object, and false otherwise.

If you are interested to find one such object, then use find instead of filter. The return value will be the first found object, or undefined when there is no such object.

The key to the better time complexity is the use of the Set. It could also be a plain object with the JSON strings as property names, and for each an arbitrary value (like true).

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!