I have my JSON as follows
{"DATA": [{"id":11,"name":"ajax","subject":"OR","mark":63},
{"id":12,"name":"javascript","subject":"OR","mark":63},
{"id":13,"name":"jquery","subject":"OR","mark":63},
{"id":14,"name":"ajax","subject":"OR","mark":63},
{"id":15,"name":"jquery","subject":"OR","mark":63},
{"id":16,"name":"ajax","subject":"OR","mark":63},
{"id":20,"name":"ajax","subject":"OR","mark":63}],"COUNT":"120"}
Is there any good method to find out the distinct name
from this JSON
Result javascript,jquery,ajax
I can do this using following methode
var arr=[''];
var j=0;
for (var i = 0; i < varjson.DATA.length; i++) {
if($.inArray(varjson.DATA[i]["name"],arr)<0){
arr[j]=varjson.DATA[i]["name"];
j++;
}
}
Is there any better method
which gave me better performance?
This question is related to
javascript
jquery
json
This is a great spot for a reduce
var uniqueArray = o.DATA.reduce(function (a, d) {
if (a.indexOf(d.name) === -1) {
a.push(d.name);
}
return a;
}, []);
Underscore.js is great for this kind of thing. You can use _.countBy()
to get the counts per name
:
data = [{"id":11,"name":"ajax","subject":"OR","mark":63},
{"id":12,"name":"javascript","subject":"OR","mark":63},
{"id":13,"name":"jquery","subject":"OR","mark":63},
{"id":14,"name":"ajax","subject":"OR","mark":63},
{"id":15,"name":"jquery","subject":"OR","mark":63},
{"id":16,"name":"ajax","subject":"OR","mark":63},
{"id":20,"name":"ajax","subject":"OR","mark":63}]
_.countBy(data, function(data) { return data.name; });
Gives:
{ajax: 4, javascript: 1, jquery: 2}
For an array of the keys just use _.keys()
_.keys(_.countBy(data, function(data) { return data.name; }));
Gives:
["ajax", "javascript", "jquery"]
First we can just run map()
function to get the new array with the results of calling a provided function on every element in the varjson.DATA
.
varjson.DATA.map(({name})=>name))
After getting the array of name
from the varjson.DATA
. We can convert it into a set that will discard all duplicate entries of array and apply spread operator to get a array of unique names:
[...new Set(varjson.DATA.map(({name})=>name))]
const varjson = {_x000D_
"DATA": [{_x000D_
"id": 11,_x000D_
"name": "ajax",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
},_x000D_
{_x000D_
"id": 12,_x000D_
"name": "javascript",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
},_x000D_
{_x000D_
"id": 13,_x000D_
"name": "jquery",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
},_x000D_
{_x000D_
"id": 14,_x000D_
"name": "ajax",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
},_x000D_
{_x000D_
"id": 15,_x000D_
"name": "jquery",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
},_x000D_
{_x000D_
"id": 16,_x000D_
"name": "ajax",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
},_x000D_
{_x000D_
"id": 20,_x000D_
"name": "ajax",_x000D_
"subject": "OR",_x000D_
"mark": 63_x000D_
}_x000D_
],_x000D_
"COUNT": "120"_x000D_
}_x000D_
_x000D_
console.log( [...new Set(varjson.DATA.map(({name})=>name))]);
_x000D_
Give this a go:
var distinct_list
= data.DATA.map(function (d) {return d[x];}).filter((v, i, a) => a.indexOf(v) === i)
As you can see here, when you have more values there is a better approach.
temp = {}
// Store each of the elements in an object keyed of of the name field. If there is a collision (the name already exists) then it is just replaced with the most recent one.
for (var i = 0; i < varjson.DATA.length; i++) {
temp[varjson.DATA[i].name] = varjson.DATA[i];
}
// Reset the array in varjson
varjson.DATA = [];
// Push each of the values back into the array.
for (var o in temp) {
varjson.DATA.push(temp[o]);
}
Here we are creating an object with the name
as the key. The value is simply the original object from the array. Doing this, each replacement is O(1) and there is no need to check if it already exists. You then pull each of the values out and repopulate the array.
NOTE
For smaller arrays, your approach is slightly faster.
NOTE 2
This will not preserve the original order.
Use Jquery Method unique.
var UniqueNames= $.unique(data.DATA.map(function (d) {return d.name;}));
alert($.unique(names));
try this, MYJSON will be your json data.
var mytky=[];
mytky=DistinctRecords(MYJSON,"mykeyname");
function DistinctRecords(MYJSON,prop) {
return MYJSON.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
})
}
Source: Stackoverflow.com