过滤数组并获取第一个值作为唯一ID的对象数组

时间:2016-06-08 07:35:19

标签: javascript arrays dictionary functional-programming

我有以下数组 我需要只返回uniqe ID,例如,因为我拥有这个数组 两次id 1& 3我需要新的数组,只需要第一个findigs id,

在这个例子中,前3个条目,我如何使用map / filter而不是常规for

var oData = [{
  id: 1,
  ListTypeGroupDescription: 2,
  test: 111,
  test2: 222
}, {
  id: 2,
  ListTypeGroupDescription: 4,
  test: 333,
  test2: 444
}, {
  id: 3,
  ListTypeGroupDescription: 6,
  test: 666,
  test2: 777
}, {
  id:1,
  ListTypeGroupDescription: 99,
  test: 666,
  test2: 777
}, {
  id: 3,
  ListTypeGroupDescription: 99,
  test: 666,
  test2: 777
}];


var data = oData.map(
  function(obj) {
    return obj.id;
  }
);

http://jsfiddle.net/5qu0j8g0/1/

地图只返回ID,但我需要所有对象

我需要这个newArray

var oNew = [{
  id: 1,
  ListTypeGroupDescription: 2,
  test: 111,
  test2: 222
}, {
  id: 2,
  ListTypeGroupDescription: 4,
  test: 333,
  test2: 444
}, {
  id: 3,
  ListTypeGroupDescription: 6,
  test: 666,
  test2: 777
}
]

5 个答案:

答案 0 :(得分:4)

您可以使用Array#filter和临时哈希表Object.create(null))(一个没有任何原型的真正空对象)对其进行过滤,在使用this解决的回调中。

  

filter() 方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。

var oData = [{ id: 1, ListTypeGroupDescription: 2, test: 111, test2: 222 }, { id: 2, ListTypeGroupDescription: 4, test: 333, test2: 444 }, { id: 3, ListTypeGroupDescription: 6, test: 666, test2: 777 }, { id: 1, ListTypeGroupDescription: 99, test: 666, test2: 777 }, { id: 3, ListTypeGroupDescription: 99, test: 666, test2: 777 }],
    oNew = oData.filter(function (a) {
        if (!this[a.id]) {
            this[a.id] = true;
            return true;
        }
    }, Object.create(null));

console.log(oNew);

ES6

var oData = [{ id: 1, ListTypeGroupDescription: 2, test: 111, test2: 222 }, { id: 2, ListTypeGroupDescription: 4, test: 333, test2: 444 }, { id: 3, ListTypeGroupDescription: 6, test: 666, test2: 777 }, { id: 1, ListTypeGroupDescription: 99, test: 666, test2: 777 }, { id: 3, ListTypeGroupDescription: 99, test: 666, test2: 777 }],
    oNew = oData.filter((temp => a => !temp[a.id] && (temp[a.id] = true))(Object.create(null)));

console.log(oNew);

答案 1 :(得分:1)

这可行。

var keys = [];
var data = oData.filter(
  function(obj) {
    if(keys.indexOf(obj.id) == -1) {
       keys.push(obj.id)
       return obj;
    }
  }
);

注意:我已将.map()更改为.filter()

答案 2 :(得分:1)

这是Unique values in an array的修改版本。它为Array原型添加了一个 getUniqueByID 函数,该函数返回数组中对象ID的唯一值(注意:这仅适用于数组被定义为像你的那样):

Array.prototype.getUniqueByID = function(){
   var u = {}, a = [];
   for(var i = 0, l = this.length; i < l; ++i){
      if(u.hasOwnProperty(this[i].id)) {
         continue;
      }
      a.push(this[i]);
      u[this[i].id] = 1;
   }
   return a;
}

console.log(oData.getUniqueByID());

答案 3 :(得分:1)

Array.map函数将返回一个与初始数组具有相同长度(大小)的新数组。
但是您需要过滤/保留具有唯一id属性的元素。 /> 使用ids函数将辅助数组(thisArg列表)用作第二个参数(Array.forEach):

var oNew = [];

oData.forEach(function(obj){
    if (this.indexOf(obj['id']) === -1) {  // check if current 'id' was processed previously 
        this.push(obj['id']);
        oNew.push(obj);
    }
}, []);

console.log(JSON.stringify(oNew, 0, 4));

输出:

[
    {
        "id": 1,
        "ListTypeGroupDescription": 2,
        "test": 111,
        "test2": 222
    },
    {
        "id": 2,
        "ListTypeGroupDescription": 4,
        "test": 333,
        "test2": 444
    },
    {
        "id": 3,
        "ListTypeGroupDescription": 6,
        "test": 666,
        "test2": 777
    }
]

答案 4 :(得分:1)

使用ECMAScript 6:

var oData = [{ id: 1, ListTypeGroupDescription: 2, test: 111, test2: 222 }, { id: 2, ListTypeGroupDescription: 4, test: 333, test2: 444 }, { id: 3, ListTypeGroupDescription: 6, test: 666, test2: 777 }, { id: 1, ListTypeGroupDescription: 99, test: 666, test2: 777 }, { id: 3, ListTypeGroupDescription: 99, test: 666, test2: 777 }],
    oNew = oData.filter((obj, index, self) => self.findIndex((o) => { return o.id === obj.id; }) === index);

console.log(oNew);