1

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"}
]
5
  • in the second sample why do you have type: FR ? Commented Apr 15, 2022 at 13:10
  • @jeremy-denis thanks for reply, because arrobj2.code and obj2.code matches but not type and idtype Commented Apr 15, 2022 at 13:15
  • @jeremy-denis 2nd point `then check the obj idtype and arrobj code is same or obj.idtype value exists then return that code matched array list Commented Apr 15, 2022 at 13:18
  • Second check - If obj.idtype and arrobj.code is same then obviously obj.idtype is exist. So we can chek only if obj.idtype is exist. Commented Apr 15, 2022 at 13:33
  • @A1exandrBelan thanks for reply, yes, i mean the obj.idtype value should check Commented Apr 15, 2022 at 13:47

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.