Javascript 筛选数组并获取第一个值作为唯一id的对象数组
我有以下数组 例如,我只需要返回uniqe ID,因为在这个数组中 两次id 1和3我需要一个新数组,只有第一个findigs id 在本例中,我将介绍前3个条目,以及如何将其与“贴图/过滤器”一起使用,而不是与“常规”一起使用Javascript 筛选数组并获取第一个值作为唯一id的对象数组,javascript,arrays,dictionary,functional-programming,Javascript,Arrays,Dictionary,Functional Programming,我有以下数组 例如,我只需要返回uniqe ID,因为在这个数组中 两次id 1和3我需要一个新数组,只有第一个findigs id 在本例中,我将介绍前3个条目,以及如何将其与“贴图/过滤器”一起使用,而不是与“常规”一起使用 var oData = [{ id: 1, ListTypeGroupDescription: 2, test: 111, test2: 222 }, { id: 2, ListTypeGroupDescription: 4, test: 3
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;
}
);
地图只返回ID,但我需要所有对象
我需要这个新阵列
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
}
]
这可能行得通
var keys = [];
var data = oData.filter(
function(obj) {
if(keys.indexOf(obj.id) == -1) {
keys.push(obj.id)
return obj;
}
}
);
注意:我也将
.map()
更改为.filter()
。这是从的修改版本。它向数组
原型添加了一个getUniqueByID函数,该函数按数组中对象的ID返回唯一值(注意:这只适用于定义与您类似的数组):
Array.prototype.getUniqueByID=function(){
变量u={},a=[];
对于(变量i=0,l=this.length;i
您可以使用和一个临时哈希表对象对其进行筛选。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(函数(a){
如果(!此[a.id]){
此[a.id]=true;
返回true;
}
},Object.create(null));
控制台日志(oNew)
数组。map
函数将返回一个与初始数组长度(大小)相同的新数组。但您需要过滤/保留具有唯一
id
属性的元素。使用辅助数组(用于
ID
列表)作为第二个参数(thisArg
),使用数组。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
}
]
使用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);
控制台日志(oNew)代码>请尝试我的回答数据将包含完整对象。如果您注意到,我正在使用return obj
而不是return obj.id
您不需要在filter
回调函数中返回obj
或obj.id
,您只需要返回true或false即可,然而,当您返回true时,整个对象将在新数组中可用。我认为就时间复杂性而言,它不会变得更好。使用与过滤函数集成的哈希表是很聪明的。它减少了冗余的索引
搜索。然而,我真的很喜欢你的iLife发明,因为你不能按预期使用带有箭头的temp
对象。我很感谢您尝试将所有内容都包含在筛选方法中。良好的功能性方法+
[
{
"id": 1,
"ListTypeGroupDescription": 2,
"test": 111,
"test2": 222
},
{
"id": 2,
"ListTypeGroupDescription": 4,
"test": 333,
"test2": 444
},
{
"id": 3,
"ListTypeGroupDescription": 6,
"test": 666,
"test2": 777
}
]