Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda