如何过滤具有保存值javascript的对象数组

时间:2017-03-24 09:11:37

标签: javascript arrays object

我有一个对象数组

var arr = [{ name: 'ABC', class: 'A', section:'1', roll_no: '123' },
           { name: 'xyz', class: 'A', section:'1', roll_no: '456' },
           { name: 'AKM', class: 'B', section:'2', roll_no: '333' },
           { name: 'PQR', class: 'A', section:'1', roll_no: '444' },
           { name: 'STU', class: 'B', section:'2', roll_no: '533' },
           { name: 'JKL', class: 'B', section:'2', roll_no: '987' }];

在这里,您可以看到我有classsection,我希望找到不同的classsection值。

例如,我希望得到2个A和B类,2个1和2个部分。

如何在不同的数组中获取不同的值?

感谢。

修改

我期待的输出是:

var output = [{ class: 'A', section:'1'},
              { class: 'B', section:'2'}
             ];

这不是一个重复的问题,我没有使用下划线js。

4 个答案:

答案 0 :(得分:3)

使用Set对象和Array.prototype.forEach()函数的解决方案:



var arr = [{ name: 'ABC', class: 'A', section:'1', roll_no: '123' },
    { name: 'xyz', class: 'A', section:'1', roll_no: '456' },
    { name: 'AKM', class: 'B', section:'2', roll_no: '333' },
    { name: 'PQR', class: 'A', section:'1', roll_no: '444' },
    { name: 'STU', class: 'B', section:'2', roll_no: '533' },
    { name: 'JKL', class: 'B', section:'2', roll_no: '987' }];

var s = new Set(),
    result = [];

arr.forEach(function (o) {
    var hash = o.class + o.section;
    if (!s.has(hash)) {  // ensuring the unique combination of `class`+`section` values
        result.push({class: o.class, section: o.section});
        s.add(hash);
    }
});

console.log(result);




答案 1 :(得分:1)

var arr = [{ name: 'ABC', class: 'A', section:'1', roll_no: '123' },
       { name: 'xyz', class: 'A', section:'1', roll_no: '456' },
       { name: 'AKM', class: 'B', section:'2', roll_no: '333' },
       { name: 'PQR', class: 'A', section:'1', roll_no: '444' },
       { name: 'STU', class: 'B', section:'2', roll_no: '533' },
       { name: 'JKL', class: 'B', section:'2', roll_no: '987' }];

现在,下面的arrA数组只包含类“A”的元素。

var arrA = arr.filter((element) => element.class === 'A')

在此处详细了解Array.prototype.filter方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

答案 2 :(得分:0)

我用循环做了这个,

var arr = [{ name: 'ABC', class: 'A', section:'1', roll_no: '123' },
           { name: 'xyz', class: 'A', section:'1', roll_no: '456' },
           { name: 'AKM', class: 'B', section:'2', roll_no: '333' },
           { name: 'PQR', class: 'A', section:'1', roll_no: '444' },
           { name: 'STU', class: 'B', section:'2', roll_no: '533' },
           { name: 'JKL', class: 'B', section:'2', roll_no: '987' }];
var output = [], filters = [];
contains = false;
output.push(arr[0]);
filters.push({class: arr[0].class, section: arr[0].section});
for (var i = 1; i < arr.length; i++) {
    for (var j = 0; j < filters.length; j++) {
        if (filters[j].class !== arr[i].class && filters[j].section !== arr[i].section) {
            contains = false;
        } else {
            contains = true;
            break;
        }
    }
    if (!contains) {
        filters.push({class: arr[i].class,  section: arr[i].section});
        output.push(arr[i]);
    }
}
console.log(output);

答案 3 :(得分:0)

var arr = [{ name: 'ABC', class: 'A', section:'1', roll_no: '123' },
           { name: 'xyz', class: 'A', section:'1', roll_no: '456' },
           { name: 'AKM', class: 'B', section:'2', roll_no: '333' },
           { name: 'PQR', class: 'A', section:'1', roll_no: '444' },
           { name: 'STU', class: 'B', section:'2', roll_no: '533' },
           { name: 'JKL', class: 'B', section:'2', roll_no: '987' }];

您可以使用reduce和Array.prototype的一些方法。所以,ES5决定没有循环:

var distinct = arr.reduce(function(curDistinct, curItem) {
  var bFlag =  curDistinct.some(function(curDistinctItem) {
    if (curDistinctItem.class == curItem.class && curDistinctItem.section == curItem.section)
      return true;
    else
      return false;
  });
  if (!bFlag) {
    curDistinct.push({class: curItem.class, section: curItem.section});
  }
  return curDistinct;
}, []);

有关使用过的方法的更多信息,请阅读:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some