0

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!

3 Answers 3

1

You could just use map:

x = x.map(it => ({
    index: it.index, 
    data: [it.data[0].props.children, it.data[1], it.data[2]]
}));
Sign up to request clarification or add additional context in comments.

Comments

0

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) {
    // ....
}

Comments

0

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]
   })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.