迭代数组并向新数组添加唯一参数

时间:2018-01-17 13:22:12

标签: javascript arrays ecmascript-6

我遇到了一个问题,我想就如何解决这个问题提供反馈。

这是我的JSON:

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

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

我想迭代这个问题数组并将 ALL object.categories附加到一个新数组,然后过滤掉重复的数组。基本上我的回答应该是:

["Handla", "Reklamation"]

5 个答案:

答案 0 :(得分:2)

感谢ES6 Set,您可以轻松过滤重复值。您只需要首先将类别展平为单个数组:

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

答案 1 :(得分:1)

您可以使用Service方法和ES6 map()并传播语法Set

来执行此操作

...

或者代替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),您可以使用map()并使用reduce()作为累加器参数。

Set

答案 2 :(得分:1)

您可以分两步完成。将问题减少到所有类别的数组,然后过滤唯一的项目。像这样:

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)

答案 3 :(得分:1)

您可以收集数组中的所有类别,并使用Set获取唯一值。

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

答案 4 :(得分:1)

您可以使用普通的旧JS在更多浏览器上工作

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

更现代一点:

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