I have an array to
var table = [
{
country:"india",
b:2
},
{
country:"usa",
b:33
},
{
country:"australia",
b:3
},
{
country:"india",
b:32
},
{
country:"southafrica",
b:31
},
{
country:"australia",
b:30
},
{
country:"india",
b:40
}
];
result expected :
var table = [
{
country:"india",
b:2
},
{
country:"usa",
b:33
},
{
country:"australia",
b:3
},
{
country:"southafrica",
b:31
}
];
My code is :
function getUniqueValuesOfKey(array, key){
return array.reduce(function(carry, item){
if(item[key] && !~carry.indexOf(item[key])) carry.push(item[key]);
return carry;
}, []);
}
document.write(JSON.stringify(getUniqueValuesOfKey(table, 'a')));
how to get the unique array result based on the country key