jason
jason

Reputation: 47

Typescript array foreach with push function (Angular)

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?

Upvotes: 2

Views: 5836

Answers (2)

Invictus
Invictus

Reputation: 546

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.

Upvotes: 1

shivlal kumavat
shivlal kumavat

Reputation: 888

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

Upvotes: 0

Related Questions