remove duplicates from json array javascript code example
Example 1: js delete duplicates from array
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
Example 2: Remove Duplicate objects from JSON
//change is
while( $row = mysqli_fetch_array( $result ))
{
$response[] = $row;
}
//with this
while($row = mysqli_fetch_array($result))
{
$response['id'] = $row['id'];
$response['name'] = $row['name'];
$response['password'] = $row['password'];
$response['email'] = $row['email'];
}
Example 3: javascript to remove duplicates from an array
uniqueArray = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
})
Example 4: 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);
}
});