I have a JSON, which contains id and points. if the ID matches, i need to increase with 1. IF the id does not match i need to push to a new empty array
Here what I have tried. but it is not working
var points = [{
id: 1,
point: 0
}, {
id: 2,
point: 0
}, {
id: 3,
point: 1
}]
var data = {
id: 1
}
if (!(points.length)) {
points.push(data)
} else {
if (points.find(e => e.id === data.id)) {
var m = points.find(e => e)
console.log(m.id)
m.point+= 1
} else {
points.push(data)
}
}
pointsis an array of objects.m = points.find(e => e)that is wrong. You need to do the same test in the callback as in theifcondition. Of course, you should rewrite it so to have to do that only once.datais the same as the id of the first element inpoints