Epple
Epple

Reputation: 962

How to concat array of array in Javascript

I have an array with child array and I want to concat items from data to new array like below. How I can do it?

Example:

[
    {
        "title": "Javascript 1",
        "data": [
            {
                "text": "hello world 1"
            },
            {
                 "text": "hello world 2"
            },
        ]
    },
    {
        "title": "Javascript 2",
        "data": [
            {
                "text": "hello world 3"
            },
            {
                "text": "hello world 4"
            },
        ]
    },
]

The result as expected:

[

   {
      "text": "hello world 1"
   },
   {
      "text": "hello world 2"
   },
   {
      "text": "hello world 3"
   },
   {
      "text": "hello world 4"
   },
]

Upvotes: 0

Views: 81

Answers (3)

Sampat Aheer
Sampat Aheer

Reputation: 75

Here is how you can get the desired result.

    let result = [];
    let myarr = [
    {
        "title": "Javascript 1",
        "data": [
            {
                "text": "hello world 1"
            },
            {
                 "text": "hello world 2"
            },
        ]
    },
    {
        "title": "Javascript 2",
        "data": [
            {
                "text": "hello world 3"
            },
            {
                "text": "hello world 4"
            },
        ]
    },
];

for(let i=0; i<myarr.length;i++)
{
myarr[i].data.forEach(function(values) {
    result.push(values);

});
}
console.log(result);

Hope this will help.

Upvotes: 0

Simon Jacobs
Simon Jacobs

Reputation: 6463

flatMap is your friend here (docs):

var arr = [
    {
        "title": "Javascript 1",
        "data": [
            {
                "text": "hello world 1"
            },
            {
                 "text": "hello world 2"
            },
        ]
    },
    {
        "title": "Javascript 2",
        "data": [
            {
                "text": "hello world 3"
            },
            {
                "text": "hello world 4"
            },
        ]
    },
];
console.log("before",arr);
console.log("after", arr.flatMap(x => x.data));

/* Outputs "after", [{
  text: "hello world 1"
}, {
  text: "hello world 2"
}, {
  text: "hello world 3"
}, {
  text: "hello world 4"
}] */

Upvotes: 1

tenshi
tenshi

Reputation: 26327

This is very easy with the new flatMap method:

const data = [
    {
        "title": "Javascript 1",
        "data": [
            {
                "text": "hello world 1"
            },
            {
                 "text": "hello world 2"
            },
        ]
    },
    {
        "title": "Javascript 2",
        "data": [
            {
                "text": "hello world 3"
            },
            {
                "text": "hello world 4"
            },
        ]
    },
];

const result = data.flatMap((item) => item.data);

console.log(result);

// same thing as:
console.log(data.map((item) => item.data).flat());

Upvotes: 4

Related Questions