I try to make a unique array of objects. I try a lot of solution. Finaly a write a function. It works, but there is other solution? And whay $.unique dont work?
var a = [{"id": "id1", "name": "id1"},{"id": "id1", "name": "id1"},{"id": "id2", "name": "id2"}];
var jq = $.unique(a);
console.log("jq=",jq); // NOT UNIQUE!?
var myfunc = unique(a);
console.log("myfunc=",myfunc); // UNIQUE!
function unique(arr){
var uniqueArr=[];
var jsonarr = [];
$.each(arr,function(ax, a){
var j = JSON.stringify(a);
if( jsonarr.indexOf(j) == -1 ){
jsonarr.push(j);
uniqueArr.push(a);
}
});
return uniqueArr;
}