-1

I hava a json object as

var arr=[{"id":"1","ref":"100"},{"id":"2","ref":"200"},{"id":"3","ref":"100"}]

Now I have a function such that a new json object is passed ex.{"id":"4","ref":"400"},then I need to search the arr[] whether there is any object with same ref or not.If there is no element with that ref ,then I need to push to arr.If else not.How can we do this

var arr = [];
var obj = {"id":"4","ref":"400"};
function(obj){
    if(!arr.contains(obj.ref)) //this didn't work
    {
        arr.push(obj);
    }
}

How can we check this condition whether there is an object with same ref in array.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user7350714
  • 365
  • 1
  • 6
  • 20
  • 2
    Possible duplicate of [How to determine if object is in array](http://stackoverflow.com/questions/4587061/how-to-determine-if-object-is-in-array) – Rajesh Feb 28 '17 at 10:33
  • Another one: http://stackoverflow.com/questions/22844560/check-if-object-value-exists-within-a-javascript-array-of-objects-and-if-not-add – Rajesh Feb 28 '17 at 10:34
  • The given array only contains 2 objects with same ref. i.e., 100. – chindirala sampath kumar Feb 28 '17 at 10:42

3 Answers3

3

You could use Array#includes with the right callback.

var arr = [{"id":"1","ref":"100"},{"id":"2","ref":"200"},{"id":"3","ref":"100"}],
    obj = {"id":"4","ref":"400"};


if(!arr.includes(o => o.ref === obj.ref)) {
    arr.push(obj);
}

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Note that it's ES7 feature and could not work on some [platforms](http://kangax.github.io/compat-table/es2016plus/) – barbsan Feb 28 '17 at 10:59
  • @Nina this worked..But I need to start a timercount of 5mins when an object is pushed in to arr and if the ref of that object gets updated to processed,I need to delete that object from arr..How can we do this? – user7350714 Feb 28 '17 at 11:00
  • @user7350714, you could as a new question, because this question needs more information than it could be asked and answered in comments of a different question. – Nina Scholz Feb 28 '17 at 11:10
  • @NinaScholz http://stackoverflow.com/questions/42509094/how-to-add-and-delete-an-object-in-json-array-based-on-events-cursor-observecha – user7350714 Feb 28 '17 at 13:08
1

Use function some, which can be used also in ES5

var arr = [{"id":"1","ref":"100"},{"id":"2","ref":"200"},{"id":"3","ref":"100"}]

var obj1 = {"id":"4","ref":"400"};
var obj2 = {"id":"5","ref":"200"};

function addSth(obj){
  if(!arr.some(function(el){return (el.ref === obj.ref)}))
  {
    arr.push(obj);
  }
}

addSth(obj1);
addSth(obj2);

console.log(arr)
barbsan
  • 3,418
  • 11
  • 21
  • 28
  • one morething..I need to set a time interval of 5mins once we push that object and with in this interval if ref values gets updated to processed.I need to delete that object from arr.How can we do that? – user7350714 Feb 28 '17 at 11:09
  • See function [window.setInterval](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval). – barbsan Feb 28 '17 at 11:17
  • @barbasan In the above if, in the arr object there already exists an object with same ref,how to delete that whole object from arr...slice? – user7350714 Feb 28 '17 at 11:32
  • There are many ways to achieve it. See [one related question](http://stackoverflow.com/questions/16747798/delete-duplicate-elements-from-an-array), [second](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) – barbsan Feb 28 '17 at 11:41
0

Try this. It will work

var arr = [{ "id": "1", "ref": "100" }, { "id": "2", "ref": "200" }, { "id": "3", "ref": "100" }]

obj = { "id": "4", "ref": "100" };

    var result = 0;
    for (var i = 0; i < arr.length; i++) {           
        if (arr[i].ref == obj.ref) {
            result = result + 1;
        }            
    }
    if (result == 0) {
        arr.push(obj);
    }
Ajsatis
  • 41
  • 2