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

190
Visualizações
How to fetch the array of objects with array object and array list based on conditions using javascript

I have array of objects, and arrays

how to compare based on conditions using javascript

  1. fetch the array of objects having same value property

  2. then, based on value list If check cid is same, return both array of objects

  3. or then, based on value list Else check cid is different but includes of IN or FI return empty

    or return that array object

var listcode =["IN","FI"];
var listarr1 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SP", value: "B3456"},
  {id:4, name: "ben", cid: "FI", value: "C5678"},
]

var listarr2 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"},
]

Expected Output 
//same value , and has "IN" 
//listarr1
[]

//listarr2
//same value no include of `FI or IN` so return
[
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"}
] 

I tried

const checkIdMembers = list => {
    const idlist = ['IN', 'FI'];
    const resultarray = list
      .map((obj, i) => list.find((element, index) => {
        if (i !== index && element.value === obj.value && 
           (element.cid === obj.cid || idlist.includes(element.cid)) {
          return obj;
        }
      }))
      .filter(x => x);
    return resultarray;
  };
  
  
  const result = checkIdMembers(listarr1);
 

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

0

It think it's easier if you invert the order of the operations

const listcode =["IN","FI"];
const listarr1 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SP", value: "B3456"},
  {id:4, name: "ben", cid: "FI", value: "C5678"},
]

const listarr2 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"},
]

const listarr3 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "IN", value: "C5678"},
  {id:4, name: "ben", cid: "IN", value: "C5678"},
]



const checkIdMembers = data => {

const ALL = 'ALL'
 const dataObj  = data.reduce((res, d) => {

   const existingValue = res[d.value] || {}
   const existing = existingValue[d.cid] || []
   const existingAll = existingValue[ALL] || []
   
   existingValue[d.cid] = [...existing, d]
   
   if(!listcode.includes(d.cid)){
     existingValue[ALL] = [...existingAll, d]
   }
   res[d.value] = existingValue 
   return res
 }, {})
 return Object.values(dataObj).flatMap(v => {
    return Object.values(v).filter(group => group.length > 1)
 })
 
}

console.log(checkIdMembers(listarr1))
console.log(checkIdMembers(listarr2))
console.log(checkIdMembers(listarr3))

I realize that my answer wasn't correct so i edited it.

I was missing one of your prerequisite and I had to change the approach a bit

I transformed your initial array in an object like this

{
  "<value>" : {
     "<cid>" : [...elements with same cid and value]
      ....
     "ALL"   : [...elements with same value different cid but not in listcode]
  }
}

Once you have this object you can just take the values and than use flatMap on it to merge all groups of array that have length > 1

I hope this helps

about 4 years ago · Juan Pablo Isaza Relatório

0

I don't know if I understood correctly what you need but I think you can use a filter to get an array of the needed elements. It looks like this, conditions may vary according to your needs:

const data = [{
    id: 1,
    name: "dino",
    cid: "IN",
    value: "A1234"
  },
  {
    id: 2,
    name: "hem",
    cid: "IN",
    value: "B3456"
  },
  {
    id: 3,
    name: "zen",
    cid: "SG",
    value: "C5678"
  },
  {
    id: 4,
    name: "ben",
    cid: "SP",
    value: "C5678"
  },
]

const elementsWithIN = data.filter(el => el.cid === 'IN');
const elementsWithoutINOrSP = data.filter(el => !['IN', 'SP'].includes(el.cid))

console.log('elementsWithIN: ', elementsWithIN)
console.log('elementsWithoutINOrSP: ', elementsWithoutINOrSP)

about 4 years ago · Juan Pablo Isaza Relatório

0

So if I understood correctly this is what you want to achieve.

var listcode =["IN","FI"];
var listarr1 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SP", value: "B3456"},
  {id:4, name: "ben", cid: "FI", value: "C5678"},
]

var listarr2 =[
  {id:1, name: "dino", cid: "IN", value: "A1234"},
  {id:2, name: "hem", cid: "IN", value: "B3456"},
  {id:3, name: "zen", cid: "SG", value: "C5678"},
  {id:4, name: "ben", cid: "SP", value: "C5678"},
]

//same value , and has "IN" 
//listarr1
const filterForSameIncluding = (codeToInclude, array) =>{
array = array.filter(o => codeToInclude.includes(o.cid));
var countList = array.reduce(function(p, c){
  p[c.value] = (p[c.value] || 0) + 1;
  return p;
}, {});
 return array.filter(function(obj){
  return countList[obj.value] > 1;
});
}

const filterForSameExcluding = (codeToExclude, array) =>{
array = array.filter(o => !codeToExclude.includes(o.cid));
var countList = array.reduce(function(p, c){
  p[c.value] = (p[c.value] || 0) + 1;
  return p;
}, {});
 return array.filter(function(obj){
  return countList[obj.value] > 1;
});
}
//listarr2
//same value no include of `FI or IN` so return

  
  
  const result1 = filterForSameIncluding(["IN"], listarr1);
  const result2 = filterForSameExcluding(["IN", "FI"], listarr2);

  console.log("same value with including codes=",result1)
  console.log("same value with excluding codes=", result2)

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