0

i'm trying to reduce the nested array into array of objects as i would like to split the multi-level array to single object of array

this is the array structure that i want to reduce

var array1 = [
  {
    "course": "ABC"
    "skills":["skill1", "skill2"],
  },
  {
    "course": "DEF"
    "skills":["skill1"],
  },
  {
    "course": "GHI"
    "skills":["skill1", "skill2", "skill3"],
  }
]

what i expect as an outcome

var array1 = [
  {
    "course": "ABC"
    "skill":"skill1",
  },
  {
    "course": "ABC"
    "skill":"skill2",
  },
  {
    "course": "DEF"
    "skill":"skill1",
  },
  {
    "course": "GHI"
    "skills":"skill1",
  },
  {
    "course": "GHI"
    "skills":"skill2",
  },
  {
    "course": "GHI"
    "skill": "skill3",
  }
]

5 Answers 5

3

var array1 = [
  {
    course: "ABC",
    skills:["skill1", "skill2"]
  },
  {
    course: "DEF",
    skills: ["skill1"]
  },
  {
    course: "GHI",
    skills: ["skill1", "skill2", "skill3"]
  }
];

const flatten = array =>
  array.reduce((results, item) => [...results, ...item.skills.map(skill => ({ course: item.course, skill: skill }))], []);


console.log(flatten(array1));

2

You can do this with flatMap (if it's supported where you're running your code). The idea is to map each course, and then further map each skill to the object you want and then flatten it:

var array1 = [{
    "course": "ABC",
    "skills": ["skill1", "skill2"]
  },
  {
    "course": "DEF",
    "skills": ["skill1"]
  },
  {
    "course": "GHI",
    "skills": ["skill1", "skill2", "skill3"]
  }
];

var res = array1.flatMap(({course, skills}) => skills.map(skill => ({course, skill})));
console.log(res);

2
  • 1
    I learnt something today, didn't know arrays had a flatMap method. But I think destructing in answering questions adds a layer of difficulty that is unnecessary for people learning new concepts as item => item.skills.map(skill => ({ item.course, skill: skill })) removes a huge barrier of understanding for people not up with destructing yet. Commented Jun 27, 2019 at 22:24
  • @AdrianBrand yes flatMap is quite useful. As for whether destructuring is difficult or not, you may be right about new learners. But most JS developers who have spent some time with the language would probably be familiar with the concept nowadays.
    – slider
    Commented Jun 28, 2019 at 18:26
0

You can do it by using two forEach. In the first forEach loop the outer array and in the inner forEach iterate the skills array and create an object using each element from the skills array

var array1 = [{
    "course": "ABC",
    "skills": ["skill1", "skill2"],
  },
  {
    "course": "DEF",
    "skills": ["skill1"],
  },
  {
    "course": "GHI",
    "skills": ["skill1", "skill2", "skill3"],
  }
]


let newArr = [];
array1.forEach(function(item) {


  item.skills.forEach(function(elem) {
    newArr.push({
      course: item.course,
      skill: elem
    })
  })
});
console.log(newArr)

0

You can do this using map function of array object. map traverse through each element.

var array1 = [
  {
    "course": "ABC",
    "skills":["skill1", "skill2"],
  },
  {
    "course": "DEF",
    "skills":["skill1"],
  },
  {
    "course": "GHI",
    "skills":["skill1", "skill2", "skill3"],
  }
];

let mappedArray = [];

array1.map(el=>{
  el.skills.map(skill=>{
    mappedArray.push({
      course: el.course,
      skill: skill
    });
  });
});

console.log(mappedArray);

0

Kind of the opposite of what most people want! Here you go - a version using reduce and spreading:

var array1 = [
  {
    "course": "ABC",
    "skills":["skill1", "skill2"],
  },
  {
    "course": "DEF",
    "skills":["skill1"],
  },
  {
    "course": "GHI",
    "skills":["skill1", "skill2", "skill3"],
  }
];

const res = array1.reduce((a, { skills, ...c }) => {
  skills.forEach(skill => a.push({ ...c, skills: skill }));
  return a;
}, []);

console.log(res);

1
  • 1
    Nothing will confuse a noob trying to learn about reduce like throwing in some destructuring with the rest operator and the spread operator in the same answer. Commented Jun 27, 2019 at 5:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.