1

Given the following scenario, how can I return a single array?

let array = [{
    name: "name01", 
    arr: [
      {name: "inner01"}, 
      {name: "inner02"}, 
      {name: "inner03"}
    ]
  }, {
    name: "name02", 
    arr: [
      {name: "inner04"}, 
      {name: "inner05"}, 
      {name: "inner06"}
    ]
  }
]

let convo = array.map(item => {
  return {
    name: item.name,
    ...item.arr,
  };
});

https://jsfiddle.net/3ng8dc2x/1/

I would like to get something like:

[
  {name: "name01"}, 
  {name: "inner01"}, 
  {name: "inner02"}, 
  {name: "inner03"},
  {name: "name02"},
  {name: "inner04"}, 
  {name: "inner05"}, 
  {name: "inner06"}
]

As always any and all direction is appreciated, so thanks in advance!

4 Answers 4

5

If you are writing for the latest browsers and latest node, flatMap() is perfect for this:

let array = [{
    name: "name01", 
    arr: [
      {name: "inner01"}, 
      {name: "inner02"}, 
      {name: "inner03"}
    ]
  }, {
    name: "name02", 
    arr: [
      {name: "inner04"}, 
      {name: "inner05"}, 
      {name: "inner06"}
    ]
  }
]

let flat = array.flatMap(({name, arr}) => [{name}, ...arr])
console.log(flat)

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

3 Comments

just noticed, the result is missing name01 and name02
And you missed adding name key's in output name01 and name02
@GetOffMyLawn it's a fair warning, but I think it's an overstatement to say MDN doesn't suggest using it. It just isn't supposed everywhere yet, but if it is supported where you need, it works well.
2

Array reduce might be a better option than map.

let array = [{
    name: "name01", 
    arr: [
      {name: "inner01"}, 
      {name: "inner02"}, 
      {name: "inner03"}
    ]
  }, {
    name: "name02", 
    arr: [
      {name: "inner04"}, 
      {name: "inner05"}, 
      {name: "inner06"}
    ]
  }
]

let convo = array.reduce((a, {name, arr}) => a.concat([{name}], arr), []);
console.log(convo)

2 Comments

on side note:- And you missed adding name keys in output name01 and name02
Because of the nature of my use case and the preference for concise code, this will work great! Thanks! ...and thanks for all of the different views and directions to solve for this!
1

One possible solution is to combine Array.reduce() with Object.entries and for every entry of the current inspected object, check the type to decide how to put the element on a new array:

let array = [
  {
    name: "name01", 
    surname: "surname01", 
    arr: [{name: "inner01"}, {name: "inner02"}, {name: "inner03"}]
  },
  {
    name: "name02",
    surname: "surname02",  
    arr: [{name: "inner04"}, {name: "inner05"}, {name: "inner06"}]
  }
];

let res = array.reduce((acc, curr) =>
{
    Object.entries(curr).forEach(([k, v]) =>
    {
        if (typeof v === 'string')
            acc.push({[k]: v});
        else if (Array.isArray(v))
            acc.push(...v);
    });

    return acc;
}, []);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Note, the previous is likely a generic solution, however, if you can trust on the structure of your objects, i.e, they all have a name property holding a string and an array on the property arr, then the code can be simplified:

let array = [
  {
    name: "name01", 
    arr: [{name: "inner01"}, {name: "inner02"}, {name: "inner03"}]
  },
  {
    name: "name02", 
    arr: [{name: "inner04"}, {name: "inner05"}, {name: "inner06"}]
  }
];

let res = array.reduce(
    (acc, {name, arr}) => acc.concat([{name}, ...arr]),
    []
);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Comments

0

You can use reduce and destructuring assignment

let array = [{name: "name01", arr: [{name: "inner01"}, {name: "inner02"}, {name: "inner03"}]},
{name: "name02", arr: [{name: "inner04"}, {name: "inner05"}, {name: "inner06"}]}]

let op = array.reduce((op,{name,arr}) =>op.concat([{name}],arr),[])

console.log(op)

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.