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

168
Visualizações
How to return array of objects by checking its property and arraylist in javascript

how to compare array of objects with arrays using javascript

I need to check array of objects based on conditons

  1. if the targetvalue is same and code does not have value in arraylist return array of objects

  2. else if the targetvalue is same and code same value in arraylist return array of objects

else return []

var arraylist =["IT","FI"];
var arrobj1 =[
  {id:1, name: "sonu", code: "IT", targetvalue: "908"},
  {id:2, name: "abi", code: "IT", targetvalue: "834"},
  {id:3, name: "lisa", code: "SP", targetvalue: "834"},
  {id:4, name: "ben", code: "FI", targetvalue: "234"},
]

Expected Output 
//same value , and has "IT" 
[]

****

var arrobj2 =[
  {id:1, name: "sonu", code: "IT", targetvalue: "908"},
  {id:2, name: "abi", code: "IT", targetvalue: "834"},
  {id:3, name: "lisa", code: "SG", targetvalue: "234"},
  {id:4, name: "ben", code: "SP", targetvalue: "234"},
]

Expected Output
//same targetvalue and no include of `FI or IT` so return
[
 {id:3, name: "lisa", code: "SG", targetvalue: "234"},
  {id:4, name: "ben", code: "SP", targetvalue: "234"},
] 


****

var arrobj3 =[
  {id:1, name: "sonu", code: "IT", targetvalue: "908"},
  {id:3, name: "lisa", code: "FI", targetvalue: "234"},
  {id:4, name: "ben", code: "FI", targetvalue: "234"},
]
Expected Output
// targetvalue and code same
 [ {id:3, name: "lisa", code: "FI", targetvalue: "234"},
  {id:4, name: "ben", code: "FI", targetvalue: "234"}]

I tried

const checkIdList = list => {
    const idlist = ['IN', 'FI'];
    const resultarray = list
      .map((obj, i) => list.find((elem, index) => {
        if (i !== index && elem.targetvalue === obj.targetvalue && 
           (element.code === obj.code || idlist.includes(element.code)) {
          return obj;
        }
      }))
      .filter(x => x);
    return resultarray;
  };
var finalResult = this.checkIdList(arrobj1);
  
  

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can start by grouping the elements by targetValue and then filter the Object.values of the result by your three conditions:

function checkIdList(array, list) {
  // group by targetValue
  const grouped = {};
  for (const o of array) {
    (grouped[o.targetvalue] ??= []).push(o);
  }

  return Object.values(grouped)
    .filter(g =>
      g.length > 1  // subarrays of length > 1 indicate duplicates
      && (
        g.every(({ code }) => !list.includes(code)) || // if every code is not in the list
        g.every(({ code }, _, a) => code === a[0].code) // OR include if all codes are equal and included in list
      ))
    .flat();
}

const arraylist = ["IT", "FI"];
const arrobj1 = [{ id: 1, name: "sonu", code: "IT", targetvalue: "908" }, { id: 2, name: "abi", code: "IT", targetvalue: "834" }, { id: 3, name: "lisa", code: "SP", targetvalue: "834" }, { id: 4, name: "ben", code: "FI", targetvalue: "234" },];
const arrobj2 = [{ id: 1, name: "sonu", code: "IT", targetvalue: "908" }, { id: 2, name: "abi", code: "IT", targetvalue: "834" }, { id: 3, name: "lisa", code: "SG", targetvalue: "234" }, { id: 4, name: "ben", code: "SP", targetvalue: "234" },];
const arrobj3 = [{ id: 1, name: "sonu", code: "IT", targetvalue: "908" }, { id: 3, name: "lisa", code: "FI", targetvalue: "234" }, { id: 4, name: "ben", code: "FI", targetvalue: "234" },];

console.log(checkIdList(arrobj1, arraylist));
console.log(checkIdList(arrobj2, arraylist));
console.log(checkIdList(arrobj3, arraylist));

about 4 years ago · Juan Pablo Isaza 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