基于两个属性从javascript中的对象数组中获取唯一数组

时间:2016-03-01 18:34:05

标签: javascript arrays oop object

我需要从数组对象中获取唯一值。例如,我有以下数据,

   var empDetails =[
{"name":"dinesh","age":"24","companyName":"ABC","designation":"developer"},
{"name":"dinesh","age":"24","companyName":"ABC","designation":"developer"},{"name":"dinesh","age":"24","companyName":"ABC","designation":"TestEngr"},
{"name":"Ramesh","age":"24","companyName":"ABC","designation":"developer"}];

从这个对象我想得到唯一的数组细节。 输出应该如下:

uniqueDet = [
{"name":"dinesh","age":"24","companyName":"ABC","designation":"developer"},
 {"name":"dinesh","age":"24","companyName":"ABC","designation":"TestEngr"},
    {"name":"Ramesh","age":"24","companyName":"ABC","designation":"developer"}]

根据公司名称和名称,我想获得独特的细节。 能否给我解决方案来解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

var uniqueDet = empDetails.filter(function(x, i) {
    return empDetails.map(function(e) { 
        return JSON.stringify(e)
     }).indexOf(JSON.stringify(x)) == i;
});