1

I have parkingSpots that is an array of objects which contains a property of coordinates which is also an array.

e.g index 0:

_id: "5e03c83459d0c115589067ba"
capacity: 2
description: "safas"
title: "aa"
coordinates: (2) [-34.193705512748686, 150.2320126953125]

I'm trying to find the object that has the arrayToBeFound. I tries this solution and many others but still doesn't work.

    let arrayToBeFound = [e.latLng.lat(), e.latLng.lng()];
    let selectedParkingSpot = parkingSpots.find(x => x.coordinates === arrayToBeFound);

I keep getting undefined but it exists. Any help?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user12361681
  • 97
  • 2
  • 7
  • 1
    Maybe try `parkingSpots.find(x => (x.coordinates[0] === e.latLng.lat() && x.coordinates[1] === e.latLng.lng()));` – Ben Dec 25 '19 at 22:01
  • You cannot use `===` to compare arrays, since they are essentially objects and objects are unique: one object will never be a strict equivalent to another. You will need to compare the lat and lng independently as per @kaleidawave's suggestion. – Terry Dec 25 '19 at 22:03
  • Does this answer your question? [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Heretic Monkey Dec 25 '19 at 22:08

5 Answers5

0

You need to compare two arrays by comparing each item of the first array with the corresponding item of another array. Directly comparing an array against another array will never work - they always be different, because JS compares arrays (objects) by their reference. Only if both references point to the same array they will be equal. The primitives however are compared by value.

let selectedParkingSpot = parkingSpots.find(x => x.coordinates[0] === arrayToBeFound[0] &&  x.coordinates[1] === arrayToBeFound[1]);
Andrej
  • 1,679
  • 1
  • 26
  • 40
0

I think this is the easiest way

let selectedParkingSpot = parkingSpots.find(x => x.coordinates[0] === arrayToBeFound[0] && x.coordinates[1] === arrayToBeFound[1]);
Jora
  • 191
  • 2
  • 13
0

You can use the answers being posted here, I like them more except if you are going to add values to that array, or perform comparisons between other arrays and/or object, or if you don't care about your array's order.

Otherwise, you can use Lodash's isEqual to compare objects by their values.

tcanusso
  • 192
  • 7
0

$(document).ready(function() {

    function arraysEqual(a, b) {
      if (a === b) return true;
      if (a == null || b == null) return false;
      if (a.length != b.length) return false;

      for (var i = 0; i < a.length; ++i) {
        if (a[i] !== b[i]) return false;
      }
      return true;
    }

    var arrayOfObjects  = [{
        _id: "5e03c83459d0c115589067ba",
        capacity: 2,
        description: "safas",
        title: "aa",
        coordinates: [-34.193705512748686, 160.2320126953125],
    },
    {
        _id: "5e03c83459d0c115589067ba",
        capacity: 2,
        description: "safas",
        title: "aa",
        coordinates: [-34.193705512748686, 150.2320126953125],
    }];


    var arrayToBeFound = [-34.193705512748686, 150.2320126953125];

    $.each(arrayOfObjects, function(index, object){
        if(arraysEqual(arrayToBeFound, object.coordinates)){
            console.log(arrayOfObjects[index]);
        }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Ahed Kabalan
  • 815
  • 6
  • 8
-1

You can't compare 2 arrays but if you stringify them it will work.

const data = [
    {
        _id: "5e03c83459d0c115589067ba",
        capacity: 2,
        description: "safas",
        title: "aa",
        coordinates: [-34.193705512748686, 150.2320126953125]
    }
]
const arrayToBeFound = [-34.193705512748686, 150.2320126953125]

let spot = data.find((ele)=>{
    return JSON.stringify(ele.coordinates) === JSON.stringify(arrayToBeFound)
})

// returns the object
console.log(spot)

// returns false
console.log(data[0].coordinates == arrayToBeFound)

//returns true
console.log(typeof data[0].coordinates === typeof arrayToBeFound)

The reason why you got undefined is because the comparison of x.coordinates == arrayToBeFound returns false there fore find won't return any thing and since selectedParkingSpot holds no value it will be undefined.

fubar
  • 383
  • 2
  • 11
  • Please don’t recommend to stringify an object for comparison. If you have a large dataset, stringify will be very inefficient. – Kyle Dec 25 '19 at 22:44