'how to get such an array [{ id: '198', included: true }] from such an array [{ '198': true }]
how to get newArray from this array forEach,map
const array = [
{ id: '198', included: true },
{ id: '199', included: true },
{ id: '202', included: false },
];
const newObj = { '198': true , '199': true , '202': false };
Solution 1:[1]
User reduce function to create an Object from an array.
const array = [
{ id: "198", included: true },
{ id: "199", included: true },
{ id: "202", included: false },
];
const result = array.reduce((acc, curr) => {
acc[curr.id] = curr.included;
return acc;
}, {});
console.log(result);
Solution 2:[2]
If you want get newObj from array like your body question. Try this with reduce is shorten
const array = [
{ id: '198', included: true },
{ id: '199', included: true },
{ id: '202', included: false },
];
const newObj = array.reduce((prev, curr) => ({...prev, [curr.id]: curr.included}),{})
console.log(newObj)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rahul Sharma |
| Solution 2 |
