Home > Mobile >  Check if an array includes some objects
Check if an array includes some objects

Time:11-10

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},
]

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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).

  • Related