1

I want to compare the value of a particular key in my JSON array with new value to check whether the value exists or not.

For example, I have an array:

[
    { name: abc, num: 121212 },
    { name: bcd, num: 21212 },
    { name: def, num: 111222 }
]

Now a new value comes which I want to check. Does that name already exist? If it does, then I only want to update the number and if not then I want to push the object in the array.

Here is my code:

if ((Dnum.num).includes(number)) {
    console.log("inside if");
    console.log(Dnum.indexOf(number)); 
} else {
    Dnum.push({num:number,
        lat:lat,
        lng:lng,
        name:name
    });
}
Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55

3 Answers3

0

Problem:

Actually .includes() and .indexOf() methods won't work with objects, they should be used with an array of strings or Numbers as they use strict equality to compare the elements and objects can't be compared this way, so you need to implement this logic by yourself.

Solution:

You need to check if an object matching the searched name already exists in the array, update the num value of this object, otherwise if no object matches the searched name, push the new object to the array:

if (arr.some(function(obj) {
    return obj.name === searchedVal.name;
  })) {
  arr.forEach(function(el, index) {
    if (el.name === searchedVal.name) {
      el.num += searchedVal.num;
      found = true;
    }
  });
} else {
  arr.push(searchedVal);
}

Demo:

var arr = [{
  name: "abc",
  num: 121212
}, {
  name: "bcd",
  num: 21212
}, {
  name: "def",
  num: 111222
}];

var searchedVal = {
  name: "abc",
  num: 5
};

if (arr.some(function(obj) {
    return obj.name === searchedVal.name;
  })) {
  arr.forEach(function(el, index) {
    if (el.name === searchedVal.name) {
      el.num += searchedVal.num;
      found = true;
    }
  });

} else {
  arr.push(searchedVal);
}
console.log(arr);

If you don't want to use .some() method, you can do it this way:

var searchedVal = {
  name: "abc",
  num: 5
};
var found = false;
arr.forEach(function(el, index) {
  if (el.name === searchedVal.name) {
    el.num+= searchedVal.num;
    found = true;
  }
});

if (!found) {
  arr.push(searchedVal);
}
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Well, your problem (if I understand correctly) is that you want to use includes() but what you actually want to accomplish doesn't correspond to what the method does. You want to find if there's an object with a certain name in your array already, not if it contains a known element. Something like this:

var data = [{name: 'abc', num: 121212}, {name: 'bcd', num: 21212}, {name: 'def', num: 111222}];

function addOrUpdate(newElement, data) {
    var i;
    for (i = 0; i < data.length; i++) {
        if (data[i].name == newElement.name) {
            data[i] = newElement;
            return;
        }
    }
    data.push(newElement);
}

addOrUpdate({name: 'bcd', num: 131313}, data);
console.log(data);
addOrUpdate({name: 'new', num: 131313}, data);
console.log(data);
klumme
  • 608
  • 3
  • 8
-1

Use Array.prototype.find():

var res = Dnum.find(function (item) {
  return item.num === number;
});

if (res) {
  console.log("inside if");
  console.log(res);
  res.num = number; 
} else {
Dnum.push({
  num:number,
      lat:lat,
      lng:lng,
      name:name
  });
}
Andrzej Smyk
  • 1,684
  • 11
  • 21