'How to get new array of objects based on array object and array in javascript
For the array object and arrays,
return the array of objects only when
same value and dont have any model that matches
arrayitemsame value and has same model whether matches or not in arrayitem
return [] when
- same value and has atleast one model exists in the
arrayitem
var arrayitem =["car", "bike"];
var arrobj1=[
{id:1, name: "xys", model:"car", value: 200},
{id:2, name: "abc", model:"bike", value: 200},
{id:3, name: "tex", model:"plane", value: 300},
]
Expected Output:
// value same and model exists in arrays
[]
*****
var arrobj2=[
{id:1, name: "xys", model:"car", value: 200},
{id:2, name: "abc", model:"plane", value: 300},
{id:3, name: "tes", model:"plane", value: 300},
]
Expected Output:
// both value and model same
[
{id:2, name: "abc", model:"plane", value: 300},
{id:3, name: "tes", model:"plane", value: 300}
]
***
var arrobj3=[
{id:1, name: "xys", model:"car", value: 200},
{id:2, name: "abc", model:"bike", value: 300},
{id:3, name: "tes", model:"plane", value: 300},
]
Expected Output
//same value and alteast one matches the arrays
[]
Tried
const resultarray = arrobj1
.map((obj, i) => {
return arrobj1.find((element, index) => {
if (i !== index && element.value === obj.value) {
if(element.model !== obj.model &&
(!arrayitem.includes(element.model) && !arrayitem.includes(obj.model))){
return obj;
} if(element.model === obj.model) {
return obj;
}
}
})
})
.filter(x => x);
return resultarray;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
