We would like to know how to remove Objects to keep unique key within an array.
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!-- w w w . ja v a 2 s . co m-->
var a = [
{ id:"123", url:"example1.com"},
{ id:"123", url:"example2.com"},
{ id:"456", url:"example3.com"},
{ id:"789", url:"example4.com"},
];
var b = [];
var t = {};
for(var i = 0; i < a.length; i++){
if(t[a[i].id]){
continue;
}
t[a[i].id] = true;
b.push(a[i]);
}
document.writeln(JSON.stringify(b));
</script>
</head>
<body>
</body>
</html>
The code above is rendered as follows: