const arrData = [{x: {value: 1}, y: {value: 2}}, {z: {value: 3}}]
How can I merge all the objects under each array object as shown below?
const arrData = [{x: 1, y: 2}, {z: 3}]
const arrData = [{x: {value: 1}, y: {value: 2}}, {z: {value: 3}}]
How can I merge all the objects under each array object as shown below?
const arrData = [{x: 1, y: 2}, {z: 3}]
Looking for something like this?
"use strict"
const arrData = [{x: {value: 1}, y: {value: 2}},{z: {value: 3}}];
// (1) mapping all element in arrData
// (2) looping over all entries (e as entry) of element
// (3) return new object 'o' with keys (k) as key and values (object[key].value = v) as values
const res = arrData.map(e => (globalThis.o={}, Object.entries(e).map(([k, v]) => o[k]=v.value.toString()), o))
console.log(res)
const arrData = [ {x: {value: 1}, y: {value: 2}}, {z: {value: 3}}]
const res = arrData.map(e => // iterate over array
Object.keys(e) // iterate over keys in e (x, y, z)
.reduce((acc, key) => { // transform {key: object} into {key: value}
acc[key] = e[key].value // transform value object value into value
return acc // and merge the keys
}, {})
)
console.log(res)
let result = arrData.map(i =>
Object.entries(i).reduce((acc,el) => {
acc[el[0]]=el[1].value;
return acc;
}
, {}))
console.log(result);
This maps each object in the array to a new one where each of the key-value pairs have been mapped so that the values are plucked out of the original innermost objects.
The re_entry function returns a new function that will convert objects to entries (key - value pairs), uses a supplied function to convert those entries and then creates a new object from those entries.
const arrData = [{x: {value: 1}, y: {value: 2}},{z: {value: 3}}];
const re_entry = (fn) => (obj) => Object.fromEntries( fn(Object.entries(obj)) );
const pluck_value_for_entry = ([k, { value }]) => [k, value];
const re_entry_for_value = re_entry((entries) => entries.map(pluck_value_for_entry));
const res = arrData.map(re_entry_for_value);
console.log(res);