code123
code123

Reputation: 67

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"}
]

Upvotes: 1

Views: 170

Answers (2)

A1exandr Belan
A1exandr Belan

Reputation: 4780

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; }

Upvotes: 1

Daniel Santana
Daniel Santana

Reputation: 1847

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.

Upvotes: 2

Related Questions