简体   繁体   English

如何在对象内部找到数组

[英]How to find array inside object

I have parkingSpots that is an array of objects which contains a property of coordinates which is also an array.我有parkingSpots ,它是一个对象数组,它包含一个坐标属性,它也是一个数组。

eg index 0:例如索引 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 .我试图找到具有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?有什么帮助吗?

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.直接将一个数组与另一个数组进行比较是行不通的——它们总是不同的,因为 JS 通过它们的引用来比较数组(对象)。 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]);

我认为这是最简单的方法

let selectedParkingSpot = parkingSpots.find(x => x.coordinates[0] === arrayToBeFound[0] && x.coordinates[1] === arrayToBeFound[1]);

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.否则,您可以使用Lodash 的 isEqual来比较对象的值。

 $(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>

You can't compare 2 arrays but if you stringify them it will work.您无法比较 2 个数组,但如果将它们字符串化,它将起作用。

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.您未定义的原因是因为x.coordinates == arrayToBeFound的比较返回 false,因此 find 不会返回任何东西,并且由于 selectedParkingSpot 没有值,它将是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM