Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
How can I see if Object Array has element in Another Object Array?

Is there a way to tell if an object array has any common elements to another object array, and what that object intersect is? (like a Contains function). In the example below,ProductId3 in Object Array 1, is also contained in Object Array 2.

I'm thinking of using a double for loop . However is there a more efficient/optimal way, or shorthand ecma or lodash function?

array1.forEach(arr1 => {
  array2.forEach(arr2 => { 
       if (arr1.productId === arr2.productId && 
           arr1.productName === arr2.productName ...

checking all object members, not just ProductId

Object Array 1:

[
{
    ProductId: 50,
    ProductName: 'Test1',
    Location: 77,
    Supplier: 11,
    Quantity: 33
},
{
    ProductId: 3,
    ProductName: 'GHI',
    Location: 1,
    Supplier: 4,
    Quantity: 25
}
]

Object Array 2:

[
{
    ProductId: 1,
    ProductName: 'ABC',
    Location: 3,
    Supplier: 4,
    Quantity: 52
},
{
    ProductId: 2,
    ProductName: 'DEF',
    Location: 1,
    Supplier: 2,
    Quantity: 87
},
{
    ProductId: 3,
    ProductName: 'GHI',
    Location: 1,
    Supplier: 4,
    Quantity: 25
},
{
    ProductId: 4,
    ProductName: 'XYZ',
    Location:  5,
    Supplier: 6,
    Quantity: 17
}
]
almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

For a simple yet reasonably fast solution, you can (1) use a Set of productIds from the first array, then (2) filter the second array based on the ids from the first one, this you only have to go over each array once O(n).

let arr1 = [
  {
    ProductId: 50,
    ProductName: "Test1",
    Location: 77,
    Supplier: 11,
    Quantity: 33,
  },
  {
    ProductId: 3,
    ProductName: "GHI",
    Location: 1,
    Supplier: 4,
    Quantity: 25,
  },
];

let arr2 = [
  {
    ProductId: 1,
    ProductName: "ABC",
    Location: 3,
    Supplier: 4,
    Quantity: 52,
  },
  {
    ProductId: 2,
    ProductName: "DEF",
    Location: 1,
    Supplier: 2,
    Quantity: 87,
  },
  {
    ProductId: 3,
    ProductName: "GHI",
    Location: 1,
    Supplier: 4,
    Quantity: 25,
  },
  {
    ProductId: 4,
    ProductName: "XYZ",
    Location: 5,
    Supplier: 6,
    Quantity: 17,
  },
];

const getCommonItems = (arr1, arr2) => {
  let firstIdSet = new Set(arr1.map((product) => product.ProductId)); //1
  return arr2.filter((product) => firstIdSet.has(product.ProductId)); //2
};

console.log(getCommonItems(arr1, arr2));

almost 4 years ago · Santiago Trujillo Relatório

0

Is there a way to tell if an object array has any common elements to another object array ? - Yes you can achieve this with the help of Array.some() method. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false.

const array1 = [{
  ProductId: 50,
  ProductName: 'Test1',
  Location: 77,
  Supplier: 11,
  Quantity: 33
}, {
  ProductId: 3,
  ProductName: 'GHI',
  Location: 1,
  Supplier: 4,
  Quantity: 25
}];

const array2 = [{
  ProductId: 1,
  ProductName: 'ABC',
  Location: 3,
  Supplier: 4,
  Quantity: 52
}, {
  ProductId: 2,
  ProductName: 'DEF',
  Location: 1,
  Supplier: 2,
  Quantity: 87
}, {
  ProductId: 3,
  ProductName: 'GHI',
  Location: 1,
  Supplier: 4,
  Quantity: 25
}, {
  ProductId: 4,
  ProductName: 'XYZ',
  Location:  5,
  Supplier: 6,
  Quantity: 17
}];

const isCommonProducts = array2.some(({ ProductId }) => array1.map(obj => obj.ProductId).includes(ProductId));

console.log(isCommonProducts);

If you want to get the common object, You can achieve that with the help of Array.filter() method.

const array1 = [{
  ProductId: 50,
  ProductName: 'Test1',
  Location: 77,
  Supplier: 11,
  Quantity: 33
}, {
  ProductId: 3,
  ProductName: 'GHI',
  Location: 1,
  Supplier: 4,
  Quantity: 25
}];

const array2 = [{
  ProductId: 1,
  ProductName: 'ABC',
  Location: 3,
  Supplier: 4,
  Quantity: 52
}, {
  ProductId: 2,
  ProductName: 'DEF',
  Location: 1,
  Supplier: 2,
  Quantity: 87
}, {
  ProductId: 3,
  ProductName: 'GHI',
  Location: 1,
  Supplier: 4,
  Quantity: 25
}, {
  ProductId: 4,
  ProductName: 'XYZ',
  Location:  5,
  Supplier: 6,
  Quantity: 17
}];

const getFilteredProducts = array2.filter(({ ProductId }) => array1.map(obj => obj.ProductId).includes(ProductId));

console.log(getFilteredProducts);

almost 4 years ago · Santiago Trujillo Relatório

0

If we can assume that each array's elements (we will call them sub-arrays), which are arrays with keys, contain exactly the same keys in the same order, then this is my idea:

  1. Convert each array into a new array whose elements are the JSON representations of the original sub-array's values. This is an o(N) operation performed twice.
  2. Of the new, converted arrays find the shortest one. Convert the other into a set. This is also o(N).
  3. For each element of the shorter converted array, check to see if the set contains this value. This is also o(N).

let arr1 = [
{
    ProductId: 50,
    ProductName: 'Test1',
    Location: 77,
    Supplier: 11,
    Quantity: 33
},
{
    ProductId: 3,
    ProductName: 'GHI',
    Location: 1,
    Supplier: 4,
    Quantity: 25
}
];

let arr2 = [
{
    ProductId: 1,
    ProductName: 'ABC',
    Location: 3,
    Supplier: 4,
    Quantity: 52
},
{
    ProductId: 2,
    ProductName: 'DEF',
    Location: 1,
    Supplier: 2,
    Quantity: 87
},
{
    ProductId: 3,
    ProductName: 'GHI',
    Location: 1,
    Supplier: 4,
    Quantity: 25
},
{
    ProductId: 4,
    ProductName: 'XYZ',
    Location:  5,
    Supplier: 6,
    Quantity: 17
}
];

// Convert each sub-array's values to JSON string:
let arr1New = arr1.map(function(arr) {return JSON.stringify(Object.values(arr));});
let arr2New = arr2.map(function(arr) {return JSON.stringify(Object.values(arr));});

// Find shortest array of JSON strings:
const l1 = arr1New.length;
const l2 = arr2New.length;
// enumerate shortest list
let list, set, l, arr;
if (l1 <= l2) {
    list = arr1New;
    set = new Set(arr2New);
    l = l1;
    arr = arr1;
}
else {
    list = arr2New;
    set = new Set(arr1New);
    l = l2;
    arr = arr2;
}

for(let i = 0; i < l; i++) {
    if (set.has(list[i])) {
        console.log(arr[i]);
    }
}

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda