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

117
Visualizações
Find index of array of objects inside another array

I have 2 array of objects with as shown below.
I want to find the index by searching the child array 'sub' in parent array 'array1' keeping id as the key.

I want to search sub array in array1 and find the index of values like [0,1,2,3].

Hence my expected output should be [0,1,2,3].

//parent array    
 array1 = [
    { "id": "1111", "name": "a1"},
    { "id": "2222", "name": "a2" },
    { "id": "3333", "name": "a3" },
    { "id": "4444", "name": "a4" },
    {"id": "5555", "name": "a5" },
];


//child array
 sub = [
    { "id": "1111"},
    { "id": "2222" }        
];

I'm struggling with find the child array on the parent array as it includes the array of objects with key values.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You could use a Set for all id of sub and then filter the indices for seen id of array1.

const
    array1 = [{ id: "1111", name: "a1" }, { id: "2222", name: "a2" }, { id: "3333", name: "a3" }, { id: "4444", name: "a4" }, { id: "5555", name: "a5" }],
    sub = [{ id: "1111" }, { id: "2222" }],
    subIds = new Set(sub.map(({ id }) => id)),
    result = [...array1.keys()].filter(i => subIds.has(array1[i].id));

console.log(result);

about 4 years ago · Santiago Trujillo Relatório

0

You can compare two array in for loop like below.

const array1 = [
      { id: "1111", name: "a1" },
      { id: "2222", name: "a2" },
      { id: "3333", name: "a3" },
      { id: "4444", name: "a4" },
      { id: "5555", name: "a5" },
    ];
    
    //child array
    const sub = [{ id: "1111" }, { id: "2222" }];
    
    const indexes = [];
    
    for (let i = 0; i <= sub.length - 1; i++) {
        const subID = sub[i].id;
      for (let j = 0; j <= array1.length - 1; j++) {
          const parentID = array1[j].id;
    
          if(parentID === subID){
              indexes.push(j)
          }
      }
    }
about 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