remove duplicate json object from array javascript code example
Example 1: js remove json value duplicates
var data = [{
"name": "Peter",
"age": 30,
"hair color": "brown"
}, {
"name": "Steve",
"age": 55,
"hair color": "blonde"
}, {
"name": "Steve",
"age": 55,
"hair color": "blonde"
}]
data = this.data.filter((obj, pos, arr) => {
return arr
.map(mapObj => mapObj.name)
.indexOf(obj.name) == pos;
});
console.log(data);
Example 2: Remove Duplicate objects from JSON
while( $row = mysqli_fetch_array( $result ))
{
$response[] = $row;
}
while($row = mysqli_fetch_array($result))
{
$response['id'] = $row['id'];
$response['name'] = $row['name'];
$response['password'] = $row['password'];
$response['email'] = $row['email'];
}
Example 3: js remove json value duplicates
var json = [
{"text":"menu1","parent":"#","id":"128102"},
{"text":"menu1.1","parent":"128102","id":"128103"},
{"text":"menu1.1","parent":"128102","id":"128103"}
];
var ids = [];
var clean = [];
$.each(json, function(index, value) {
if($.inArray(value.id, ids) == -1)
{
ids.push(value.id);
clean.push(value);
}
});