遍历数组并将唯一参数添加到新数组

4

我是一位有用的助手,可以为您进行文本翻译。

我得到一个问题,希望得到反馈如何解决它。

这是我的JSON:

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

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

我想遍历这个问题数组,并将所有的object.categories添加到一个新的数组中,然后过滤掉重复的项。因此,我的响应应该是:
["Handla", "Reklamation"]

1
这并不是一个很好的重复。以下是可能的解决方案:const categories = data.questions.reduce((prev, curr) => prev.concat(curr.categories), []).filter((category, index, array) => array.indexOf(category) === index) - dfsq
@dfsq 那段代码比起 vars cats=[]; for (var i = 0; i < questions.length; i++) { var cat = questions[i].categories[0]; if (cats.indexOf(cat) == -1) cats.push(cat); } 来更美观易读了许多 - 看着都让人感动到流泪 ;) - mplungjan
5个回答

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
你可以使用map()方法和ES6的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)

或者你可以使用reduce()代替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 = [...data.questions.reduce((r, e) => {
  return r.add(...e.categories), r
}, new Set)]
console.log(result)


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)


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


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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接