0

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"
   },
]

3 Answers 3

4

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());

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

Comments

1

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"
}] */

1 Comment

it works like a charm, greets way!
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.