如何从JavaScript对象数组中获取重复值?

时间:2018-06-22 17:27:22

标签: javascript arrays filter

我的问题是我需要过滤一组数据...

我的数据随身携带。.

var arrayWithDuplicates = [
    {"licenseNum": "6", "state":"NV"},
    {"licenseNum": "6", "state":"NV"},
    {"licenseNum": "6", "state":"NV"},
    {"licenseNum": "6", "state":"CA"},
    {"licenseNum": "6", "state":"CA"},
    {"licenseNum": "6", "state":"OR"},
    {"licenseNum": "6", "state":"OR"},
    {"licenseNum": "6", "state":"OR"},
    {"licenseNum": "20", "state":"NV"},
    {"licenseNum": "10", "state":"CA"},
    {"licenseNum": "10", "state":"NV"},
    {"licenseNum": "10", "state":"OR"},
    {"licenseNum": "10", "state":"CA"}
];

新数据应该是

newdata = [6, 6, 6, 20, 10, 10, 10];

我已经尝试过了。

for (i = 0; i < arrayWithDuplicates .length; i++) {
                if ((arrayWithDuplicates[i].licenseNum) === -1)) {
                    newdata .push({
                        licenseNum: arrayWithDuplicates[i].licenseNum,
                        state: arrayWithDuplicates[i].state
                    });
                }
            };

我从这些结果中得到了结果。

newdata = [6, 20, 10]

我看到了很多很棒的例子,但是仍然不能解决我的问题。非常感谢。

4 个答案:

答案 0 :(得分:2)

获取唯一条目并筛选出您需要的内容。在下面找到内联的注释以查看方法

var arrayWithDuplicates = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"},
    {"licenseNum": "10", state:"CA"}
];

json = arrayWithDuplicates.map(x => JSON.stringify(x)) // maps each array entry in stringified json, so I could get unique entries below

var result = json.filter(function(item, pos) {
    return json.indexOf(item) == pos; // gives unique entries
}).map(x => +JSON.parse(x).licenseNum) // parses back json and gives licenseNum
console.log(result)
// [6, 6, 6, 20, 10, 10]

答案 1 :(得分:1)

ES6 JS:

var arr = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"}
];

var newarr = arr.
    filter((x, i) => arr.some((y, j) => {
        return y.licenseNum == x.licenseNum && i != j
    }))
    .map(x => x.licenseNum)
    console.log(newarr)

答案 2 :(得分:0)

var arrayWithDuplicates = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"},
    {"licenseNum": "10", state:"CA"}
];

var newdata = [];
var seens = [];
for(var i = 0; i < arrayWithDuplicates.length; i++) {
  var obj = arrayWithDuplicates[i];
  var lookupKey = obj.licenseNum + "|" + obj.state;
  if(seens.indexOf(lookupKey) == -1) {
    newdata.push(obj.licenseNum);
    seens.push(lookupKey);
  }
}

console.log(newdata);

答案 3 :(得分:0)

@kiddorails在唯一功能中提出的小更改

var arrayWithDuplicates = [
    {"licenseNum": "6", state:"NV"},
    {"licenseNum": "6", state:"CA"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "6", state:"OR"},
    {"licenseNum": "20", state:"NV"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"CA"},
    {"licenseNum": "10", state:"OR"},
    {"licenseNum": "10", state:"CA"}
];

json = arrayWithDuplicates.map(x => JSON.stringify(x)) 
let result = [];
json.forEach(function(item, pos) {
    if (json.indexOf(item) == pos) {
      result.push(+JSON.parse(item).licenseNum)
    }
})
console.log(result)