I'm using promise.AllSettled to fetch data from 2 / 3 endpoints and result looks like this
Simplified results array
const results = [
{
value: {
payload: {
email: "[email protected]",
first_name: "",
last_name: "",
pk: 183,
username: "faker",
}
},
error: true,
},
{
value: {
payload: {
avatar: "https://cdn.zeplin.io/faker.png",
email: "[email protected]",
id: 183,
is_admin: false,
username: "faker",
}
},
error: false
}
]
I would like to get flat object that contains something like this, but generated dynamically (the number of the payloads may vary)
const desiredObj = Object.assign({}, results[0].value.payload, results[1].value.payload)
/*
avatar: "https://cdn.zeplin.io/faker.png",
email: "[email protected]",
first_name: "",
id: 183,
is_admin: false,
last_name: "",
pk: 183,
username: "faker"
*/
asked Sep 14, 2020 at 18:32
Verthon
3,2774 gold badges23 silver badges36 bronze badges
1 Answer 1
const results = [
{
value: {
payload: {
email: "[email protected]",
first_name: "",
last_name: "",
pk: 183,
username: "faker"
}
}, error: true
},
{
value: {
payload: {
avatar: "https://cdn.zeplin.io/faker.png",
email: "[email protected]",
id: 183,
is_admin: false,
username: "faker",
}
},
error: false
}
];
console.log(
Object.assign({}, ...results.map(x => x.value.payload))
);
— Lux
answered Sep 19, 2020 at 6:55
GirkovArpa
5,1015 gold badges19 silver badges52 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
Object.assign({}, ...results.map(x => x.value.payload))?