4

I got a issue I would like feedback on how it can be solved.

Here's my JSON:

  questions: [
    {
      question: 'lala',
      answer: 'papa',
      categories: ['Handla']

    },
    {
      question: 'xxxx',
      answer: 'yyyy',
      categories: ['Reklamation']
    },
    {
      question: 'abcefg',
      answer: 'gooooogle',
      categories: ['Reklamation']
    }
  ]

I want to iterate over this question array and append the ALL the object.categories to a new array and then filtering out the duplicated ones. So basically my response should be:

["Handla", "Reklamation"]
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Joelgullander
  • 1,624
  • 2
  • 20
  • 46
  • 1
    Not very good duplicate at all. Here is the possible solution: `const categories = data.questions.reduce((prev, curr) => prev.concat(curr.categories), []).filter((category, index, array) => array.indexOf(category) === index)`. – dfsq Jan 17 '18 at 13:28
  • @dfsq That is so much more beautiful and readable than `vars cats=[]; for (var i = 0; i < questions.length; i++) { var cat = questions[i].categories[0]; if (cats.indexOf(cat) == -1) cats.push(cat); }` - I get tears in my eyes ;) – mplungjan Jan 17 '18 at 14:11

5 Answers5

2

Thanks to the ES6 Set you can filter duplicate values easily. You just need to flatten your categories into a single array first:

const questions = [
    {
      question: 'lala',
      answer: 'papa',
      categories: ['Handla']

    },
    {
      question: 'xxxx',
      answer: 'yyyy',
      categories: ['Reklamation']
    },
    {
      question: 'abcefg',
      answer: 'gooooogle',
      categories: ['Reklamation']
    }
  ];
const flattened = questions.reduce((prev, curr) => [...prev, ...curr.categories], []); // ['Handla', 'Reklamation', 'Reklamation']
const unique = Array.from(new Set(flattened));
console.log(unique);
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
1

You can do this with map() method and ES6 Set and spread syntax ...

const data = {"questions":[{"question":"lala","answer":"papa","categories":["Handla"]},{"question":"xxxx","answer":"yyyy","categories":["Reklamation"]},{"question":"abcefg","answer":"gooooogle","categories":["Reklamation"]}]}

const result = [...new Set([].concat(...data.questions.map(o => o.categories)))]
console.log(result)

Or instead of map() you can use reduce() and use Set as accumulator parameter.

const data = {"questions":[{"question":"lala","answer":"papa","categories":["Handla"]},{"question":"xxxx","answer":"yyyy","categories":["Reklamation"]},{"question":"abcefg","answer":"gooooogle","categories":["Reklamation"]}]}

const result = [...data.questions.reduce((r, e) => {
  return r.add(...e.categories), r
}, new Set)]
console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
1

You can do it in two steps. Reduce questions to array of all categories and then filter unique items. Something like this:

const data = {
  questions: [{
      question: 'lala',
      answer: 'papa',
      categories: ['Handla']

    },
    {
      question: 'xxxx',
      answer: 'yyyy',
      categories: ['Reklamation']
    },
    {
      question: 'abcefg',
      answer: 'gooooogle',
      categories: ['Reklamation']
    }
  ]
}

const categories = data.questions
  .reduce((prev, curr) => prev.concat(curr.categories), [])
  .filter((category, index, array) => array.indexOf(category) === index)

console.log(categories)
dfsq
  • 191,768
  • 25
  • 236
  • 258
1

You could collect all categories in an array and take a Set for getting unique values.

var questions= [{ question: 'lala', answer: 'papa', categories: ['Handla'] }, { question: 'xxxx', answer: 'yyyy', categories: ['Reklamation'] }, { question: 'abcefg', answer: 'gooooogle', categories: ['Reklamation'] }],
    unique = [...new Set(questions.reduce((r, { categories: c }) => r.concat(c), []))];
  
console.log(unique);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You could use plain old JS working on many more browsers

var cats = [], questions = [{question: 'lala',answer: 'papa',categories: ['Handla']},{question: 'xxxx',answer: 'yyyy',categories: ['Reklamation']},{question: 'abcefg',answer: 'gooooogle',categories: ['Reklamation']}];

for (var i = 0; i < questions.length; i++) {
  var cat = questions[i].categories[0];
  if (cats.indexOf(cat) == -1) cats.push(cat);
}
console.log(cats);

A bit more modern:

const questions = [{question: 'lala',answer: 'papa',categories: ['Handla']},{question: 'xxxx',answer: 'yyyy',categories: ['Reklamation']},{question: 'abcefg',answer: 'gooooogle',categories: ['Reklamation']}];
let cats = [];

questions.forEach(function(q) {
  var cat = q.categories[0];
  if (cats.indexOf(cat) == -1) cats.push(cat);
});
console.log(cats);
mplungjan
  • 169,008
  • 28
  • 173
  • 236