1

I have two objects and some of their properties are identical and I need to combine these properties into a single array for another operation. Here are the objects:

  const grippers = [
    {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]

  const pallets = [
    {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]

Note that pallets and grippers are arrays, there can be more than one, so I cant just do pallets[0].relevantRegisters.R and take it from there. So it could be like this:

const grippers = [
    {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
    {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
    {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]

I want to have a final array with the combined objects from the R: arrays, like this (not the values of the ID's, but the objects that contain the ID!):

[{ID: 1}, {ID: 2}, {ID: 3}, {ID: 1}, {ID: 2}, {ID: 3}]

Here is what I have tried:

const extractedR = [
  ...pallets
    .map((pallet) => {
      return pallet.relevantRegisters.R;
    }),
  ...grippers
    .map((gripper) => {
      return gripper.relevantRegisters.R;
    }),
]

However the result from this is an array of an array each containing the IDs. [Array(3), Array(3)]

Note: I don't need just the ID's, I need the object that contains the ID's as there are other properties within it that I also need, so I need to end up with an Array of 6 objects. Instead I end up with a 2x3 Array.

If I separate the two maps into variables (discovered it while trying to debug it) and spread the variables into array then it works, and so I've tried "double spreading" inline (don't know if that even works) like [...[...pallets(..),], ...[...grippers(..)]] but it also didnt work. I need to be able to do this inline.

5 Answers 5

2

You can use flatMap

  const grippers = [
  {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]

  const pallets = [
  {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]


const extractedR = [
  ...pallets
    .flatMap((pallet:any) => {
      return pallet.relevantRegisters.R;
    }),
  ...grippers
    .flatMap((gripper:any) => {
      return gripper.relevantRegisters.R;
    }),
]

console.log(extractedR)

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

3 Comments

Thanks, this worked. Can you tell me why my approach was not working?
@Darkbound Because you were creating an array of an array since map returns one. And pallets and grippers are already arrays.
Yes map returns an array, thats why I have used the spread operator and I thought that this will flatten it, thats what got me confused and still does
0

Is this what you want?

  const grippers = [
  {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]

  const pallets = [
  {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]
  var arrayInline = [].concat(grippers[0].relevantRegisters.R).concat(pallets[0].relevantRegisters.R);
  console.log(arrayInline);
  

1 Comment

Will not work, grippers and pallets are arrays, I have updated the post with more items, but it should have been clear and I had also a note about it.
0

You can use concat function for this.

const grippers = [
  {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]

  const pallets = [
  {
      relevantRegisters: {
        R: [
          { ID: 1 },
          { ID: 2 },
          { ID: 3 },
        ],
      },
    },
  ]
  let newArray = [];
grippers.filter(e => newArray = newArray.concat(e.relevantRegisters.R))
pallets.filter(e => newArray = newArray.concat(e.relevantRegisters.R))

console.log(newArray);
  

Comments

0

You can use array reduce to get your result.

Working Code

const grippers = [{
  relevantRegisters: {
    R: [{
        ID: 1
      },
      {
        ID: 2
      },
      {
        ID: 3
      },
    ],
  },
}, ]

const pallets = [{
  relevantRegisters: {
    R: [{
        ID: 1
      },
      {
        ID: 2
      },
      {
        ID: 3
      },
    ],
  },
}, ]

console.log([...grippers.map(({
  relevantRegisters: {
    R
  }
}) => R).reduce((arr, val) => [...arr, val]), ...pallets.map(({
  relevantRegisters: {
    R
  }
}) => R).reduce((arr, val) => [...arr, val])])

Comments

0
const extracted = [...grippers[0].relevantRegisters.R, ...pallets[0].relevantRegisters.R]

with the new requirement you could do it like this

[].concat.apply([], [...grippers.map(x => x.relevantRegisters.R), ...pallets.map(x => x.relevantRegisters.R)]);

2 Comments

It will not work, they are arrays, I added just one item in the post, to save time, there can be 5, 10, 50, doesnt matter, thats why its array. It will work for a single item, but I don't need to extract it just from the first item.
you mean there could be another relevantRegisters or another R?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.