1

I hope the title is accurate but what I have is an array like this:

{
  "someValue": 1,
  "moreValue": 1,
  "parentArray": [
    {
      "id": "2222",
      "array": [
        {
          "type": "test",
          "id": "ID-100"
        },
        {
          "type": "test1",
          "id": "ID-200"
        }
      ]
    },
    {
      "id": "5555",
      "array": [
        {
          "type": "test",
          "id": "ID-100"
        },
        {
          "type": "test1",
          "id": "ID-200"
        }
      ]
    },
    {
      "id": "444",
      "array": [
        {
          "type": "test",
          "id": "ID-100"
        },
        {
          "type": "test1",
          "id": "ID-200"
        }
      ]
    }
  ]
}

And I would like to merge all "array" properties together in a new array possibly, to something that looks like this:

{
  "someValue": 1,
  "moreValue": 1,
  "array": [
    {
      "type": "test",
      "id": "ID-100"
    },
    {
      "type": "test1",
      "id": "ID-200"
    },
    {
      "type": "test",
      "id": "ID-4400"
    },
    {
      "type": "test1",
      "id": "ID-500"
    },
    {
      "type": "test",
      "id": "ID-600"
    },
    {
      "type": "test1",
      "id": "ID-700"
    }
  ]
}

What would be a good way to combine all these properties in Typescript? I'm looking for a Typescript/Angular friendly solution since this will be looped over to create elements in HTML.

5 Answers 5

2

You could use reduce method on your parentArray property to merge inner arrays:

let obj = {
      "someValue": 1,
      "moreValue": 1,
      "parentArray": [
        {
          "id": "2222",
          "array": [
            {
              "type": "test",
              "id": "ID-100"
            },
            {
              "type": "test1",
              "id": "ID-200"
            }
          ]
        },
        {
          "id": "5555",
          "array": [
            {
              "type": "test",
              "id": "ID-100"
            },
            {
              "type": "test1",
              "id": "ID-200"
            }
          ]
        },
        {
          "id": "444",
          "array": [
            {
              "type": "test",
              "id": "ID-100"
            },
            {
              "type": "test1",
              "id": "ID-200"
            }
          ]
        }
      ]
    }

    let newObj = {
        someValue: obj.someValue,
        moreValue: obj.moreValue,
        array: obj.parentArray.reduce((prev, current) => [...prev, ...current.array],[])
    }
    
    console.log(newObj)

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

Comments

1

Another way you can do this:

const obj = {
  someValue: 1,
  moreValue: 1,
  parentArray: [
    {
      id: "2222",
      array: [
        {
          type: "test",
          id: "ID-100"
        },
        {
          type: "test1",
          id: "ID-200"
        }
      ]
    },
    {
      id: "5555",
      array: [
        {
          type: "test",
          id: "ID-100"
        },
        {
          type: "test1",
          id: "ID-200"
        }
      ]
    },
    {
      id: "444",
      array: [
        {
          type: "test",
          id: "ID-100"
        },
        {
          type: "test1",
          id: "ID-200"
        }
      ]
    }
  ]
};

let result = {
  ...obj,
  array: Object.values(obj.parentArray).flatMap(arr => arr.array)
};

delete result.parentArray;

console.log(result);

1 Comment

I like the use of flatMap, but be careful that you can still support all the browsers you want to support.
1

You can use a mapper. I usually use lodash for these things: https://lodash.com/docs/4.17.15#flatMap

const destination = {
    someValue: source.someValue,
    moreValue: source.moreValue,
    array: _.flatMap(source.parentArray, (value) => value.array)
};

Comments

0

You can use map this.data.parentArray.map(item => item.array):

Try like this:

var result = {
  someValue: this.data.someValue,
  moreValue: this.data.moreValue,
  array: this.data.parentArray.map(item => item.array)
};

Working Demo

1 Comment

That solution is not entirely correct though, there's multiple arrays where OP wants one array.
0

You can try next (It will work, with "infinite" nesting):

function getChildren(arr) {
  return arr.reduce((items, item) => [...items, ...(item.array ? getChildren(item.array) : [item])], []);
}

and call with:

getChildren(your_object.parentArray);

DEMO

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.