I've the following array I need to return only the uniqe ID for example since in this array I have twice id 1 & 3 I need new array with just the first findigs id,
In this example the first 3 entries, how I can use it with maps/filter and not with regular 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;
}
);
/
The map is returning only the ID's but I need the all objects
I need this 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
}
]
I've the following array I need to return only the uniqe ID for example since in this array I have twice id 1 & 3 I need new array with just the first findigs id,
In this example the first 3 entries, how I can use it with maps/filter and not with regular 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/5qu0j8g0/1/
The map is returning only the ID's but I need the all objects
I need this 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
}
]
Share
Improve this question
edited Jun 8, 2016 at 7:37
NSS
asked Jun 8, 2016 at 7:35
NSSNSS
7953 gold badges16 silver badges34 bronze badges
1
- Please try out my answer – phreakv6 Commented Jun 8, 2016 at 7:51
5 Answers
Reset to default 4You could filter it with Array#filter
and a temporary hash table Object.create(null))
(a really empty object without any prototypes), inside the callback addressed with this
.
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
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);
This could work.
var keys = [];
var data = oData.filter(
function(obj) {
if(keys.indexOf(obj.id) == -1) {
keys.push(obj.id)
return obj;
}
}
);
Note: I have changed .map()
to .filter()
as well.
This is a modified version from Unique values in an array. It adds a getUniqueByID function to the Array
prototype which returns the unique values by ID of your object in the array (Note: This will only work for arrays which are defined like yours):
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());
Using 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);
Array.map
function will return a new array which has the same length(size) as the initial array.
But you need to filter/remain elements with unique id
attribute.
Use an auxiliary array (for ids
list) as a second argument(thisArg
) using Array.forEach
function:
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));
The output:
[
{
"id": 1,
"ListTypeGroupDescription": 2,
"test": 111,
"test2": 222
},
{
"id": 2,
"ListTypeGroupDescription": 4,
"test": 333,
"test2": 444
},
{
"id": 3,
"ListTypeGroupDescription": 6,
"test": 666,
"test2": 777
}
]