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"]
2
  • 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). Commented Jan 17, 2018 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 ;) Commented Jan 17, 2018 at 14:11

5 Answers 5

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);

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

Comments

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)

Comments

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)

Comments

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);

Comments

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);

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.