I have had a look around for a way to solve this with no luck. I am trying to push an object to an array only when this object is unique.
We have a list of data (real one much bigger) that we loop through and get only the data needed (for a drop-down box). Using this we can then push the new data we want into an array... problem is all objects are being pushed even though 2 of them are the same (the username "Chris" object). The alert should return 3 objects as 2 that are being inserted are the same (only one should go in).
Code so far:
var list = [{
ID: 1,
name: "Chris",
phone: "111",
cusID: 1
}, {
ID: 2,
name: "Alex",
phone: "222",
cusID: 2
}, {
ID: 3,
name: "Jim",
phone: "333",
cusID: 3
}, {
ID: 4,
name: "Chris",
phone: "111",
cusID: 1
}];
var filterList = [];
$.each(list, function (i, item) {
var newUser = {
name: item.name,
cusID: item.cusID
};
if ($.inArray(newUser, filterList) == -1) {
filterList.push(newUser);
}
});
alert(filterList);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Any ideas on how to solve this issue?
Note: I am also using AngularJS if this helps in anyway.