1
const my_arr = [
   {id: 1, arr: [{subId: 1, value: 1}],
   {id: 2, arr: [{subId: 2, value: 2}],
   {id: 3, arr: [{subId: 3, value: 1}],
]

how do I map over this array my_arr and then map over arr to return an array like so:

[
  {subId: 1, value: 1},
  {subId: 3, value: 1},
]

basically filtering out only where values are 1 and then returning only that sub object

I've tried doing

my_arr.map((x) => x.map((y) => y.value === 1 ? y : null)) 
7
  • 1
    flatMap and reduce might help Commented May 23, 2022 at 13:04
  • Hi Red Baron, can you clarify what you've tried & highlight which parts you're getting stuck with? For a starting point, you can look at a for loop with an if statement, or you can look into methods such as .filter() & .map(). Commented May 23, 2022 at 13:05
  • @NickParsons edited to show Commented May 23, 2022 at 13:07
  • map collects all results; it seems like you’re actually trying to filter (or reduce). Commented May 23, 2022 at 13:08
  • Your data isn't valid. You're missing } from each of those objects. Commented May 23, 2022 at 13:08

3 Answers 3

6

You can try this approach with flatMap and filter

const my_arr = [
   {id: 1, arr: [{subId: 1, value: 1}]},
   {id: 2, arr: [{subId: 2, value: 2}]},
   {id: 3, arr: [{subId: 3, value: 1}]},
]

const result = my_arr.flatMap(item => item.arr).filter(item => item.value === 1)

console.log(result)

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

Comments

1

Your current approach maps over the outer array my_arr, and then uses an inner map to map over the inner array. Since .map() always returns an array, you'll end up mapping your objects from your outer array to other arrays, which you don't want. You can instead use .flatMap() which will combine/join the returned inner arrays into one array. However, rather than using .map() as your inner method though, you should use .filter() to create an array of objects that have a value of 1, which then gets merged into your resulting outer array created by the .flatMap() method:

const my_arr = [ {id: 1, arr: [{subId: 1, value: 1}]}, {id: 2, arr: [{subId: 2, value: 2}]}, {id: 3, arr: [{subId: 3, value: 1}]}, ];

const res = my_arr.flatMap(({arr}) => arr.filter(({value}) => value === 1));
console.log(res);

Comments

1

Since you are dealing with nested structure, you will have to get little creative.

  • First you will have to filter the array.
  • Inside it, you can use .some to check if your condition matches and return matching
  • Now you have the filtered list but you still need to format your output. You can use .reduce and concat arr of every item

This will be useful if you have multiple items in arr.

const my_arr = [
   {id: 1, arr: [{subId: 1, value: 1}] },
   {id: 2, arr: [{subId: 2, value: 2}] },
   {id: 3, arr: [{subId: 3, value: 1}] },
]

const output = my_arr
  .filter(({ arr }) =>
    arr.some(({value}) => value === 1)
  ).reduce((acc, { arr }) => acc.concat(arr), [])
  
console.log(output)

2 Comments

I was thinking of doing something similar at first, but if one of the objects (such as the first one) has an arr of arr: [{subId: 1, value: 1}, {subId: 1.5, value: 2}], then {subId: 1.5, value: 2} will be included in the result, whereas I believe OP only wants objects with a value of 1
Fair point but that is inconclusive based on the data OP has shared. So I'll keep the answer just in case someone has this usecase

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.