Reputation: 1993
I have an array of objects of arrays... for example:
x=[{index:0, data:[{ab: 'c', props: {cde:{efg:'asdf'}, children:"important text" }},"0",34]},
{index:1, data:[{ab: 'd', props: {cde:{efg:'jkl;'}, children:"some more text" }},"0",35]}]
I want to transform what is in data[0] to be what is in data [0].props.children for each element in data.
data[*]['data'][0].props.children
is there a way to replace what is in data[]['data'][0] with the text in data[]['data'][0].props.children.
so the above example would become:
x=[{index:0, data:["important text","0",34]},
{index:1, data:["some more text","0",35]}]
I hope this makes sense.
Thanks!
Upvotes: 0
Views: 46
Reputation: 612
You can try with forEach and prepare data as per your need as below code it should work for you.
x.forEach(item => {
const [dataObj, dataString, dataNumber] = item.data;
item.data = [dataObj.props.children, dataString, dataNumber]
})
Upvotes: 0
Reputation: 489
You could just use map:
x = x.map(it => ({
index: it.index,
data: [it.data[0].props.children, it.data[1], it.data[2]]
}));
Upvotes: 1
Reputation: 26354
For each thing in x
, we want to set data[0]
to data[0].props.children
:
const x=[{index:0, data:[{ab: 'c', props: {cde:{efg:'asdf'}, children:"important text" }},"0",34]},
{index:1, data:[{ab: 'd', props: {cde:{efg:'jkl;'}, children:"some more text" }},"0",35]}];
x.forEach((thing) => {
thing.data[0] = thing.data[0].props.children;
});
console.log(x);
This can be achieved with a regular for
loop as well:
for (const thing of x) {
// ....
}
Upvotes: 0