0

I need to know the best method to check if the value is in the array before push it

I have an array :

coordinates: [
     {x: 160, y: 160},
     {x: 192, y: 160},
     {x: 224, y: 160},
     {x: 224, y: 192},
     ];

For each call of my function , I need to check if new coordinates is it before proceed to push.

const payload= {x: 224, y: 190}

//Next function call 

const payload = {x: 224, y: 190}   // need to refuse pushing this value

I tried filter and other JS methods without success.

for example:

coordinates.filter(c => {if(payload !== c){coordinates.push(new)});

This one doesn't work for me .

C. Tewalt
  • 2,271
  • 2
  • 30
  • 49
Sebastian
  • 21
  • 1
  • 5
  • Does this answer your question? [Object comparison in JavaScript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – huichops Jun 18 '20 at 20:24
  • Does this answer your question? [How can I check if the array of objects have duplicate property values?](https://stackoverflow.com/questions/30735465/how-can-i-check-if-the-array-of-objects-have-duplicate-property-values) – harish kumar Jun 18 '20 at 20:30

6 Answers6

0

var payload = {x: 224, y: 190};
var coordinates = [
 {x: 160, y: 160},
 {x: 192, y: 160},
 {x: 224, y: 160},
 {x: 224, y: 192},
];

var filtered = coordinates.filter(c => c.x == payload.x &&
c.y == payload.y);
if (filtered.length == 0) filtered.push(payload)

console.log("Filtered:", filtered)
wscourge
  • 10,657
  • 14
  • 59
  • 80
Jigar Gajjar
  • 183
  • 2
  • 12
0

since each coordinate is an object, you'll need to do a deep comparison to check if the object exists in your array already:

const coordinates = [{
  x: 160,
  y: 160
}, {
  x: 192,
  y: 160
}, {
  x: 224,
  y: 160
}, {
  x: 224,
  y: 192
}];
const isInArray = newCoordinate => coordinates.filter(c => newCoordinate.x === c.x && newCoordinate.y === c.y).length > 0;
console.log(isInArray({
  x: 0,
  y: 0
})) // false
console.log(isInArray({
  x: 160,
  y: 160
})) // true
Ethan Lipkind
  • 1,136
  • 5
  • 7
0

I hope this helps

var coordinates= [
     {x: 160, y: 160},
     {x: 192, y: 160},
     {x: 224, y: 160},
     {x: 224, y: 192},
     ];

const payload = {x: 225, y: 193};


for(let prop of coordinates){
 if(prop.x == payload.x || prop.y == payload.y){
  var includes = true;
 } else {
  includes = false;
 }
}

if(includes == false){
 coordinates.push(payload);
}
console.log(coordinates);
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
0

A simple checker function to check duplicate objects

var coordinates=[
     {x: 160, y: 160},
     {x: 192, y: 160},
     {x: 224, y: 160},
     {x: 224, y: 192},
];

const pushCoordinate=(obj)=>{
 var flag=0;
 coordinates.forEach((elem)=>{
   if(obj.x===elem.x && obj.y===elem.y){
     flag=1;
    }
  });
  if(flag===1){
   return false;
  }else{
   return true;
  }
};

var payload1 = {x: 224, y: 190};
var payload2 = {x:224, y:190};

if(pushCoordinate(payload1)){
 coordinates.push(payload1);
}

if(pushCoordinate(payload2)){
 coordinates.push(payload2);
}

console.log(coordinates);
Sagar Kulkarni
  • 483
  • 4
  • 15
0

you can simply use filter array method to check x,y values

and if the filtered array has no length then there are no matching values and push payload object

let filteredArr;
let coordinates = [
    {x: 160, y: 160},
    {x: 192, y: 160},
    {x: 224, y: 160},
    {x: 224, y: 192}
    ];
    
// checker fn
const checker = (obj) => filteredArr = coordinates.filter(elm => obj.x == elm.x && obj.y == elm.y);

let payload = {x: 224, y: 190};
checker(payload);

// no matching
if (filteredArr.length === 0) coordinates.push(payload);
console.log(coordinates);
Eissa Saber
  • 161
  • 1
  • 8
0

You can use some() to check whether coordinates array includes the values of payload object

const coordinates = [
  {x: 160, y: 160},
  {x: 192, y: 160},
  {x: 224, y: 160},
  {x: 224, y: 192},
];

const payload = {x: 224, y: 192};

if(!coordinates.some(({x, y}) => x === payload.x && y === payload.y)) {
  coordinates.push(payload);
}

console.log(coordinates);
zb22
  • 3,126
  • 3
  • 19
  • 34