2

I have array:

data
    Array
    (
    [0] => Array
        (
            [name0] => J
            [name1] => L
            [name2] => C
        )

    [1] => Array
        (
            [data0] => 3,1,3
            [data1] => 5,3,2
            [Nu0] => 3,0,0
            [Nu1] => 1,1,1
        )

)

I need push the data to other array

this.datas.push({'name':'A','data':(data[1].data0).'color':'#00000'}); 
this.datas.push({'name':'C','data':(data[1].Nu0).'color':'#00000'}); 
this.datas.push({'name':'B','data':(data[1].data1).'color':'#FFFFF'}); 
this.datas.push({'name':'D','data':(data[1].Nu1).'color':'#FFFFF'}); 
this.namedatas.push(data[0].name0);
this.namedatas.push(data[0].name1);

How to use loop function to do this? Also what to do if I have more data?

1
  • 2
    1. Share the data array as valid json 2. What is the other array that you are trying to push to? 3. What is the expected output. Commented Dec 19, 2020 at 10:03

2 Answers 2

1

In your case I suggest you to use a map or a filter function:

metadas = datas.map((el) => {
    return el.name;
});

In this case in metadatas array you will have all names contained into datas array.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do easily like below in this example we are going to push data from first array items to second array newItems:

Like this is your first array items:

const items = ['item1', 'item2', 'item3']

This is your second array newItems:

const newItems = []

Here is your solution :

items.forEach(function(item){
  newItems.push(item)
})

2 Comments

if I need the data0 then null0 then data1 then null1 in the new array, how to us loop to do it?
You have do like patten, loop inside loop, it means subarray loop.

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.