Home > OS >  How to get array of objects by object and arrays in javascript
How to get array of objects by object and arrays in javascript

Time:04-18

For the array of object, compare with object and list array using javascript

should get the array of objects based on below conditions

  1. itemvalue and idvalue same, from that check arrobj cid has same codevalue return both array of objects

  2. itemvalue and idvalue are same, from that check if cid not matching the arraylist and codevalue has value that does not exist in arraylist, return array of objects

  3. itemvalue and idvalue same, from that check if codevalue matching the arraylist and cid has value that does not exist in arraylist, return array of objects

return empty array [] if above fails

//data1
var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
  {id:1, name: "sun", cid: "IN", itemvalue: "3456"},
  {id:2, name: "mon", cid: "FI", itemvalue: "4567"},
  {id:3, name: "tues", cid: "SP", itemvalue: "4567"},
  {id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"}

Expected Output
[
{id:2, name: "mon", cid: "FI", itemvalue: "4567"},  
{id:3, name: "tues", cid: "SP", itemvalue: "4567"}
]

***
//data2
var larrylist= ["IN","FI","FR"];
var arrobj2 = [
  {id:1, name: "sun", cid: "IN", itemvalue: "1234"},
  {id:2, name: "mon", cid: "FI", itemvalue: "2468"},
  {id:3, name: "tues", cid: "IN", itemvalue: "2468"},
  {id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"}

Expected Output

[]

***

//data2
var arraylist= ["IN","FI","FR"];
var arrobj3 = [
  {id:1, name: "sun", cid: "IN", itemvalue: "1234"},
  {id:2, name: "mon", cid: "FI", itemvalue: "2468"},
  {id:3, name: "tues", cid: "SG", itemvalue: "2468"},
  {id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"}

Expected Output
[
 {id:2, name: "mon", cid: "FI", itemvalue: "2468"}
]

Tried

const result = arrobj1.filter((item) => {
  return item.itemvalue === obj.idvalue &&
    (
      !arraylist.includes(item.cid)
      || item.cid === obj.codevalue
    )
})

CodePudding user response:

Making a clear problem statement will very often get you most of the way to a solution. Try to say precisely what you mean, then try to code precisely what you said.

Doing that here using the OP's problem statement and examples, we get:

The goal is to filter an array of objects (arrobj1) to produce a another array which meets criteria relating to an object (obj1) and another array (arraylist).

  1. The filter predicate is true if there's match between the prop idvalue of obj1 and the prop itemvalue of the array element, AND...

  2. The predicate must also (2a) find match between the prop codevalue of obj1 and the prop cid of the array element, BUT (2b) this comparison is contingent on the presence of either object's prop (codevalue or cid) in arraylist. Specifically, if either the element's cid or the object's codevalue is in the arraylist, then the element's cid value must match the codevalue of obj1.

Restated with this sort of (laborious) precision, coding the predicate is much easier. Let's code it in bite-sized pieces...

// condition 1
// el is an element from arrobj1, and obj is an instance like obj1
// check if their itemvalue and idvalue (respectively) are equal
const idsMatch = (el, obj) => obj.idvalue === el.itemvalue;

// condition 2b
// el is an element from arrobj1, obj is an instance like obj1 and cidList 
// is the given list of cids (like arraylist)
// check if the element's cid and the obj's codevalue is in the arraylist
const cidFound = (el, obj, cidList) => {
  return cidList.includes(el.cid) || cidList.includes(obj.codevalue);
}

// condition 2a
// el is an element from arrobj1, and obj is an instance like obj1
// check if their cid and codevalue (respectively) are equal
const cidPropMatch = (el, obj) => obj.codevalue === el.cid;

// from 2a and 2b, make condition 2
// cidsMatch if the element isn't in the cidList, or if it is and the objects' props match
const cidsMatch = (el, obj, cidList) => {
  return !cidFound(el, obj, cidList) || cidPropMatch(el, obj);
}

// combining into a predicate
const filterPredicate = (el, obj, cidList) => {
  return idsMatch(el, obj) && cidsMatch(el, obj, cidList);
};

Ta-dah! Demo...

// condition 1
const idsMatch = (el, obj) => obj.idvalue === el.itemvalue;

// condition 2b
const cidFound = (el, obj, cidList) => {
  return cidList.includes(el.cid) || cidList.includes(obj.codevalue);
}
// condition 2a
const cidPropMatch = (el, obj) => obj.codevalue === el.cid;

// condition 2
const cidsMatch = (el, obj, cidList) => {
  return !cidFound(el, obj, cidList) || cidPropMatch(el, obj);
}

// predicate
const filterPredicate = (el, obj, cidList) => {
  return idsMatch(el, obj) && cidsMatch(el, obj, cidList);
};

var arraylist = ["IN","FP", "FN"];
var arrobj1 = [
  {id:1, name: "sun", cid: "IN", itemvalue: "3456"},
  {id:2, name: "mon", cid: "FI", itemvalue: "4567"},
  {id:3, name: "tues", cid: "SP", itemvalue: "4567"},
  {id:4, name: "thurs", cid: "FI", itemvalue: "2345"},
]
var obj1 = { id:5, name: "ben", codevalue: "SG", idvalue:"4567"}

const result = arrobj1.filter(el => filterPredicate(el, obj1, arraylist));
console.log(result);

// Expected Output
// [
// {id:2, name: "mon", cid: "FI", itemvalue: "4567"},  
// {id:3, name: "tues", cid: "SP", itemvalue: "4567"}
// ]


arraylist = ["IN","FI","FR"];
var arrobj2 = [
  {id:1, name: "sun", cid: "IN", itemvalue: "1234"},
  {id:2, name: "mon", cid: "FI", itemvalue: "2468"},
  {id:3, name: "tues", cid: "IN", itemvalue: "2468"},
  {id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj2 = { id:5, name: "ben", codevalue: "SP", idvalue:"2468"}

const result2 = arrobj2.filter(el => filterPredicate(el, obj2, arraylist));
console.log(result2);

// Expected Output
// []


arraylist = ["IN","FI","FR"];
var arrobj3 = [
  {id:1, name: "sun", cid: "IN", itemvalue: "1234"},
  {id:2, name: "mon", cid: "FI", itemvalue: "2468"},
  {id:3, name: "tues", cid: "SG", itemvalue: "2468"},
  {id:4, name: "thur", cid: "FI", itemvalue: "2345"},
]
var obj3 = { id:5, name: "ben", codevalue: "FI", idvalue:"2468"}

const result3 = arrobj3.filter(el => filterPredicate(el, obj3, arraylist));
console.log(result3);

// Expected Output
// [
//  {id:2, name: "mon", cid: "FI", itemvalue: "2468"}
// ]

The important point here is the method. Try to say precisely what you mean, try to code precisely what you said. I did that here, and, believe it or not, the snippet matched the expected output on the first run.

CodePudding user response:

I think this is what you want to do :

const result = arrobj.filter((item) =>
    item.itemvalue === obj.idvalue && (arraylist.includes(item.cid) || item.cid === obj.codevalue)
);
  • Related