Push only unique elements in an array

I have an array object (x) that stores json objects (key, value). I need to make sure x only accepts a json object with a unique key. Below is an example "id" so I don't want to store other json objects with the "item1" key.

x = [{"id":"item1","val":"Items"},{"id":"item1","val":"Items"},{"id":"item1","val":"Items"}]    

var clickId = // could be "item1", "item2"....
var found = $.inArray(clickId, x);  //
if(found >=0)
{
    x.splice(found,1);
}
else{
    x.push(new Item(clickId, obj)); //push json object
}

      

+3


source to share


4 answers


will this accomplish what you are looking for? https://jsfiddle.net/gukv9arj/3/



x = [
    {"id":"item1","val":"Items"},
    {"id":"item1","val":"Items"},
    {"id":"item2","val":"Items"}
];    

var clickId = [];
var list = JSON.parse(x);
$.each(list, function(index, value){
    if(clickId.indexOf(value.id) === -1){
        clickId.push(value.id);
    }
});

      

+5


source


You cannot use inArray()

because you are looking for object

.

I would recommend rewriting the custom find using the Array.some()

following.



var x = [{"id":"item1","val":"Items"},{"id":"item1","val":"Items"},{"id":"item1","val":"Items"}]    

var clickId = "item1";
var found = x.some(function(value) {
  return value.id === clickId;
});
alert(found);
      

Run codeHide result


0


source


This is how I would do it in pure javascript.

var x = [{"id":"item1","val":"Items"},{"id":"item1","val":"Items"},{"id":"item1","val":"Items"}];

function unique(arr, comparator) {
  var uniqueArr = [];
  for (var i in arr) {
    var found = false;
    for (var j in uniqueArr) {
      if (comparator instanceof Function) {
        if (comparator.call(null, arr[i], uniqueArr[j])) {
          found = true;
          break;
        }
      } else {
        if (arr[i] == uniqueArr[j]) {
          found = true;
          break;
        }
      }
    }
    if (!found) {
      uniqueArr.push(arr[i]);
    }
  }
  return uniqueArr;
};

u = unique(x, function(a,b){ return a.id == b.id; });
console.log(u);

y = [ 1,1,2,3,4,5,5,6,1];
console.log(unique(y));
      

Run codeHide result


0


source


JS objects are great tools for keeping track of unique elements. If you start with an empty object, you can gradually add keys / values. If an object already has a key for a given element, you can set it to some known value, which is used to indicate a unique element.

Then you can loop over the object and push the unique elements into the array.

var itemsObj = {};
var itemsList = [];
x = [{"id":"item1","val":"foo"},
    {"id":"item2","val":"bar"},
    {"id":"item1","val":"baz"},
    {"id":"item1","val":"bez"}];

for (var i = 0; i < x.length; i++) {
    var item = x[i];
    if (itemsObj[item.id]) {
        itemsObj[item.id] = "dupe";
    }
    else {
        itemsObj[item.id] = item;
    }
}

for (var myKey in itemsObj) {
    if (itemsObj[myKey] !== "dupe") {
        itemsList.push(itemsObj[myKey]);
    }
}

console.log(itemsList);

      

See a working example here: https://jsbin.com/qucuso

If you want a list of elements containing only the first instance of id, you can do this:

var itemsObj = {};
var itemsList = [];
x = [{"id":"item1","val":"foo"},
    {"id":"item2","val":"bar"},
    {"id":"item1","val":"baz"},
    {"id":"item1","val":"bez"}];

for (var i = 0; i < x.length; i++) {
    var item = x[i];
    if (!itemsObj[item.id]) {
        itemsObj[item.id] = item;
        itemsList.push(item);
    }
}

console.log(itemsList);

      

0


source







All Articles