0

I want to create an array that does not have repeated object(all objects are unique) based on object property and if it is unique I want to push it to this array. Before sending it to my function I use:

vm.initResponse = angular.fromJson(response);

That push $$hashKey to each object.

    function initDropDown(list){
        console.log('initDropDown', list);
        for (var key in list) {
            //remove angularjs $$hashKey
            var obj = JSON.parse(angular.toJson(list[key]));
            //the object I want to push into new array
            var filter = {};
            filter.prop = obj.SUGNIA;
            //if indexOf returns -1 the array does not have this object
            if(vm.stockOptionsSelect.indexOf(filter) < 0){
                vm.stockOptionsSelect.push(filter);
            }

        }
    }

the list array has many objects with different SUGNIA and I want to create an array with all unique ones.

I have removed $$hashKey has explained remove-hashKey and using indexOf - indexOf.

List data -

[
  {
    "uid": "23",
    "time": "2017-10-01 16:00:35.515",
    "SUGNIA": "stock",
    "SECURITYNAME": "IBM",
    "INDEX_IN_UI": 5
  },
  {
    "uid": "29",
    "SECURITYNUM": 629014,
    "time": "2017-09-28 16:32:26.432",
    "SUGNIA": "holdings",
    "SECURITYNAME": "FaceBook",

    "INDEX_IN_UI": 10
  },
  {
    "uid": "id",
    "time": "2017-09-28 16:33:20.25",
    "SUGNIA": "stock",
    "SECURITYNAME": "google",
    "INDEX_IN_UI": 0
  }
]

And my result should be :

[{"prop":"stock"}, {"prop":"holding"}]

But I get duplicates.

Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114

3 Answers3

2

The indexOf function does the comparison using strict equality === and since the object you're passing to this function is an object that was just created, it will always return -1.

To fix that, you can use the ...some(...) function instead.

Here is an example:

if(!vm.stockOptionsSelect.some(e => e.prop == obj.SUGNIA)){
    vm.stockOptionsSelect.push({prop:obj.SUGNIA});
}
Titus
  • 22,031
  • 1
  • 23
  • 33
1

You can't use indexof object as that isn't looking at object properties but it's looking for reference to that filter object. Use some instead and look for identical object properties.

var hasItem = vm.stockOptionsSelect.some(item) {
    return item.prop === filter.prop
}
if(!hasItem) {
    vm.stockOptionsSelect.push(filter);
}

If you cant use some

var results = vm.stockOptionsSelect.filter(item) {
    return item.prop === filter.prop;
}
if(results.length === 0) {
    vm.stockOptionsSelect.push(filter);
}
andrzej.szmukala
  • 942
  • 11
  • 14
1

You can use Set and array#map to get the unique SUGNIA value from your array list.

var list = [{"uid": "23","time": "2017-10-01 16:00:35.515","SUGNIA": "stock","SECURITYNAME": "IBM","INDEX_IN_UI": 5},{"uid": "29","SECURITYNUM": 629014,"time": "2017-09-28 16:32:26.432","SUGNIA": "holdings","SECURITYNAME": "FaceBook","INDEX_IN_UI": 10},{"uid": "id","time": "2017-09-28 16:33:20.25","SUGNIA": "stock","SECURITYNAME": "google","INDEX_IN_UI": 0}];

var result = [...new Set(list.map(x => x.SUGNIA))]
             .map(x => ({prop: x}));
             
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51