-1

I have an array that has another array inside.

[
    [
        {
          "userId": 1,
          "title": "title 1",
        },
        {
          "userId": 2,
          "title": "title 2",
        }
    ],
    [
        {
          "userId": 3,
          "title": "title 3",
        }
    ]
]

I am trying to get a new array with userId only. For e.g.

[
  { "userId": 1 },
  { "userId": 2 },
  { "userId": 3 }
]

array.map(o => o.userId) works for array of objects and don't know how can i get inside array.

Any help is appreciated

3

5 Answers 5

2

you'll have to flat the array first :

const data = [
    [
        {
          "userId": 1,
          "title": "title 1",
        },
        {
          "userId": 2,
          "title": "title 2",
        }
    ],
    [
        {
          "userId": 3,
          "title": "title 3",
        }
    ]
]

const result = data.flat().map(({userId}) => ({userId}));
console.log(result);

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

2 Comments

Literally half way through typing this same answer. This is the easiest way to achieve your outcome.
Same answer. Ahhhh!!! 😭
2

You can flatten the array using array#concat and then using destructuring and array#map generate the array.

const data = [ [ { "userId": 1, "title": "title 1", }, { "userId": 2, "title": "title 2", } ], [ { "userId": 3, "title": "title 3", } ] ],
      result = [].concat(...data).map(({userId}) => ({userId}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

Array.prototype.flat is fairly new; in case you can't use it you can use a combination of reduce and map:

const data = [
    [
        {
          "userId": 1,
          "title": "title 1",
        },
        {
          "userId": 2,
          "title": "title 2",
        }
    ],
    [
        {
          "userId": 3,
          "title": "title 3",
        }
    ]
]

const userIds = data.reduce((_, a) => {
	return _.concat(a.map(({ userId }) => ({ userId })))
}, [])

console.log(userIds)

A benefit about map within the reduce call is that you're only iterating over the array once instead of chaining. This will have better performance over larger arrays than chaining array methods.

All assuming your data structure is only one level deep!

4 Comments

You're iterating once for the enclosing array and then iterating each element with .map . If anything I expect that this would be marginally slower because it needs to create a function for each inner element iteration.
@NikKyriakides not that jsperf is the end-all for the subject, but I decided to run this through and check flat vs this method.. At least in this case, reduce is much faster.
Really? Interesting. Can't check your benchmark now but if you say so.
Honestly it surprised me, too!
1

Another one using Array.reduce, for browsers that don't support Array.flat.

const data = [
  [
    {
      "userId": 1,
      "title": "title 1",
    },
    {
      "userId": 2,
      "title": "title 2",
    }
  ],
  [
    {
      "userId": 3,
      "title": "title 3",
    }
  ]
]

const result = data.reduce((arr, i) => {
  return arr.concat(i.map(({ userId }) => ({ userId })))
}, [])

console.log(result)

Comments

-1

Just get everything out in a new array :)

let arr = [
    [
        {
          "userId": 1,
          "title": "title 1",
        },
        {
          "userId": 2,
          "title": "title 2",
        }
    ],
    [
        {
          "userId": 3,
          "title": "title 3",
        }
    ]
]

let newArr = []
arr.forEach(i => i.forEach(o => newArr.push(o)))
console.log(newArr.map(o => o.userId))

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.