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

202
Visualizações
how to get array of objects based on object list in javascript

I have array of object and object in which have to

  1. check the arrobj code and obj value are same,

  2. if above condition ok, then check the obj idtype and arrobj code

is same or obj.idtype value exists then return array list or return []

of object in javascript

var arrobj = [
  {id:1, code: "brown", type: "FR"},
  {id:3, code: "black", type: "SP"},
 {id:4, code: "blue", type: "FR"}
]

var obj={
  id:20, idtype: "SG", value: "blue"
}

Expected Output
// arrobj.code and obj.value matches, obj.idtype and arraobj.type not same
[]

var arrobj2 = [
  {id:1, code: "brown", type: "FR"},
  {id:3, code: "green", type: "SP"},
 {id:4, code: "blue", type: "FR"},
{id:5, code: "blue", type: "SG"}
]

var obj2={
  id:20, idtype: "SG", value: "blue"
}

Expected Output
// arrobj.code and obj.value matches, obj.idtype value exists in arraobj.type
[
{id:4, code: "blue", type: "FR"},
{id:5, code: "blue", type: "SG"}
]
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you want to filter a javascript object array (code: equal and type: non equal) you can use the filter method.

const sampleCars1 = [
  {id: 1, code: "brown", type: "FR"},
  {id: 2, code: "black", type: "SP"},
  {id: 3, code: "green", type: "Y1"},
  {id: 4, code: "blue", type: "FR"}
];

const sampleCars2 = [
  {id: 1, code: "brown", type: "FR"},
  {id: 2, code: "black", type: "X1"},
  {id: 3, code: "green", type: "SP"},
  {id: 4, code: "blue", type: "FR"},
  {id: 5, code: "blue", type: "SG"},
  {id: 6, code: "blue", type: "A1"},
  {id: 7, code: "blue", type: "A2"}
];

const filterCars = (cars, desiredCode, undesiredType) => {
  return cars.filter(item => 
                        item.code === desiredCode && item.type !== undesiredType);
}

const filterSampleLists = () => {
  const filter = {id:20, type: "SG", code: "blue"};
  const filteredSample1 = filterCars(sampleCars1, filter.code, filter.type);
  const filteredSample2 = filterCars(sampleCars2, filter.code, filter.type);

  console.log(filteredSample1);
  console.log(filteredSample2);
}

filterSampleLists();

Mozilla Documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Similar Questions:

Find object by id in an array of JavaScript objects

Get JavaScript object from array of objects by value of property

Bonus:

  • always use meaningful names
  • use let/const instead of var. Check let statement for more.
about 4 years ago · Santiago Trujillo Relatório

0

It seems to fit your conditions

const arrobj1 = [{id:1, code: "brown", type: "FR"},{id:3, code: "black", type: "SP"},{id:4, code: "blue", type: "FR"}];
const obj1 = { id:20, idtype: "SG", value: "blue" };

const arrobj2 = [{id:1, code: "brown", type: "FR"},{id:3, code: "green", type: "SP"},{id:4, code: "blue", type: "FR"},{id:5, code: "blue", type: "SG"}];
const obj2 = { id:20, idtype: "SG", value: "blue" };

const advFilter = (arr, obj) => {
    const filtered = arr.filter(({ code }) => code === obj.value);
    const types = filtered.map(({ type }) => type);
    return types.includes(obj.idtype) ? filtered : [];
};

console.log(advFilter(arrobj1, obj1));
console.log(advFilter(arrobj2, obj2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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