1

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?

JSFiddle Demo

Note: I am also using AngularJS if this helps in anyway.

furkick
  • 423
  • 6
  • 18

3 Answers3

1

Based on Object comparison in JavaScript try something like so:

$.each(list, function (i, item) {
    var newUser = {
        name: item.name,
        cusID: item.cusID
    };
    var jsonFL = $.map(filterList, function(v,i) {
        return JSON.stringify(v);
    });
    jsonFL.indexOf( JSON.stringify(newUser) ) > -1 || filterList.push(newUser);
});
console.log(filterList);

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
  };
  var jsonFL = $.map(filterList, function(v,i) {
    return JSON.stringify(v);
  });
  jsonFL.indexOf(JSON.stringify(newUser)) > -1 || filterList.push(newUser);
});
console.log(filterList);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Community
  • 1
  • 1
PeterKA
  • 24,158
  • 5
  • 26
  • 48
  • I think this has worked, just going to do some tests. Thank you very much for your time! – furkick Sep 23 '15 at 15:03
  • Glad you found this useful. Enjoy! :-) – PeterKA Sep 23 '15 at 15:56
  • So turns out this doesn't work all that well. After getting more data it started breaking. Take a look at your example [**here**](http://jsfiddle.net/jnru4157/) – furkick Sep 28 '15 at 15:24
  • You're absolutely right; I found the bug and updated my code. The `.push()` line should really be `jsonFL.indexOf(JSON.stringify(newUser)) > -1 || filterList.push(newUser);` Take a look at this [**updated demo**](http://jsfiddle.net/fiddleyetu/jnru4157/1/) – PeterKA Sep 28 '15 at 15:40
  • Thank you! You have been a great help. – furkick Sep 29 '15 at 08:11
0

Prerequisite:

Considering the following getUserKey function, that will be our basis to distinguish users:

function getUserKey (user) {
    return [user.name, user.phone, user.cusID].join('_');
}

Explanation: we are using the keys name, phone and cusID to define unicity, though in your case I guess the key ``cusID` is enough.

Put the users in an object indexed by the 'userkey':

var users = {};
list.forEach(function (user) {
    users[getUserKey(user)] = {name: user.name, cusID: user.cusID};
});

If needed, get an array of the users.

var filterList = [];
Object.keys(users).forEach(function (key) {
    filterList.push(users[key])
});

I wrote 'if needed', because in angular you could iterate over the users object.

DEMO

Michel
  • 26,600
  • 6
  • 64
  • 69
-1

Here you go:

https://jsfiddle.net/u2w0by4v/11/

$.each(list, function (i, item) {

    var newUser = {
        name: item.name,
        cusID: item.cusID
    };
      var zUser = newUser.name+","+newUser.cusID;

    if ($.inArray(zUser, filterList) == -1) {
        filterList.push(zUser);
    }
});

The issue was the structure of your array. It was a multidimensional array, so the check wasn't working. Basically, I just turned the newUser object into a comma separated string, and passed that to your filterList array.

Mark
  • 4,773
  • 8
  • 53
  • 91
  • I can see what you have done but this is not really a good answer. I need to use the objects after eg. `filterList[0].name`. – furkick Sep 23 '15 at 14:54