Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!