I have this javascript object :
{
{
long_name: "10",
types: [
0: "street_number"
],
},
{
long_name: "street",
types: [
0: "route"
],
},
{
long_name: "Paris",
types: [
0: "locality"
],
},
...
}
And I want to flatten it and have something like :
{
street_number: "10",
route: "street",
locality: "Paris",
...
}
I am using ES6, but can't manage to flatten it this much, All I've succeeded to do is having :
{
{street_number: "10"},
{route: "street"},
{locality: "Paris"},
...
}
Here is what I Tried :
const flattenedObject = originalObject.map(flatten);
...
function flatten(element) {
let obj = {};
obj[element.types[0]] = element.long_name;
return obj;
}
Thanks for any help.
You could use
Array#reduce
with a computed property and the first element only from the array.The key feature is
Object.assign
for adding properties to the result object.