I am generating a dynamic array like below. What I wanted to achieve is to compare the first set with the second and return the common items based on id into a new array. Also the pending and required value should be grater than 0.
For example, Case : 1
let arr = [
{
0: { id: 1, name: "A", required: 1, pending: 1 },
1: { id: 2, name: "B", required: 0, pending: 0 }
},
{
0: { id: 1, name: "A", required: 1, pending: 1 },
1: { id: 2, name: "B", required: 1, pending: 1 },
2: { id: 3, name: "C", required: 0, pending: 0 }
}
]
The result would be A in this case as follows.
[
{ id: 1, name: "A", required: 1, pending: 1 }
]
Another case, Case : 2 since the array is dynamically generated. So the following can be possible.
let arr = [
{
0: { id: 1, name: "A", required: 1, pending: 1 },
1: { id: 2, name: "B", required: 1, pending: 1 },
2: { id: 3, name: "C", required: 0, pending: 0 }
}
]
The expected output of this would be
[
{ id: 1, name: "A", required: 1, pending: 1 },
{ id: 2, name: "B", required: 1, pending: 1 }
]
The below function gives the desired output for Case 1. This function not works for Case 2. Can anybody help me to solve this?
let test = arr .reduce((p, c) => p.filter(e =>c.some(s => s.id === e.id && s.pending> 0 && e.pending> 0 && s.required> 0 && e.required> 0)));
{ { ... }, { ... } }is not valid, did you mean[ { ... }, { ... } ]?