简体   繁体   English

如何检查 object 是否在 Javascript 的数组中?

[英]How do I check if an object is within an array in Javascript?

I am getting stuck on a project where I am trying to pull out relevant data from a super massive Google Maps Timeline json, and running into the problem of the structure not being as orderly as I thought it was.我被困在一个项目中,我试图从超大规模的谷歌地图时间线 json 中提取相关数据,并遇到了结构不像我想象的那样有序的问题。 Essentially, I am trying to pull out an address, time, date and mileage out of this json for every trip in my car.从本质上讲,我试图为我的汽车每次旅行从 json 中提取地址、时间、日期和里程。 to use this data, I pasted it into a normal javascript file and named it so I can use it as an object.为了使用这些数据,我将它粘贴到一个普通的 javascript 文件中并命名它,以便我可以将它用作 object。 I then take this data and create a string that will format that info like a CSV file.然后我获取这些数据并创建一个字符串,该字符串将像 CSV 文件一样格式化该信息。

From going over the structure of the json by looking at only a few trips, I was able to determine the following general structure:通过查看 json 的结构,我能够确定以下一般结构:

const google = {
   timelineObjects: [
      0: {activitySegment: {A NUMBER OF OBJECTS}},
      1: {placeVisit : {A NUMBER OF OBJECTS}},
      2: {activitySegment: {A NUMBER OF OBJECTS}},
      3: {placeVisit : {A NUMBER OF OBJECTS}}
   ]
}

activitySegment has all the travelling info, like distance, travel times, etc. placeVisit has info about the destination. activitySegment 包含所有旅行信息,例如距离、旅行时间等。 placeVisit 包含有关目的地的信息。 In my small sample, I was able to just loop through each using an if statement with i%2=0, and just change what I wanted to pull out from each, which worked well.在我的小样本中,我能够使用 i%2=0 的 if 语句循环遍历每个,然后更改我想从每个中提取的内容,效果很好。

When I tried adding a larger sample, I was finding that Google occasionally did not create a activitySegment object and only had a placeVisit, which was throwing "Uncaught TypeError: Cannot read property 'distance' of undefined".当我尝试添加更大的样本时,我发现 Google 偶尔没有创建一个 activitySegment object 并且只有一个 placeVisit,它抛出“Uncaught TypeError: Cannot read property 'distance' of undefined”。

I am sure that the even/odd sorting will not work out any more.我确信偶数/奇数排序将不再有效。 Is there a way to use a conditional to show if google[timelineObjects][i] is either a {activitySegment} or {placeVisit}?有没有办法使用条件来显示 google[timelineObjects][i] 是 {activitySegment} 还是 {placeVisit}? Or, is there a better way to figuring out what object is next in the array?或者,有没有更好的方法来确定阵列中的下一个 object 是什么?

You can test to see if the Object at the particular array index has a given property using Object.prototype.hasOwnProperty() :您可以使用Object.prototype.hasOwnProperty()测试特定数组索引处的 Object 是否具有给定属性:

 const google = { timelineObjects: [ {activitySegment: {}}, {placeVisit: {}}, {activitySegment: {}}, {placeVisit: {}} ] }; console.log(google.timelineObjects[0].hasOwnProperty("activitySegment")); // true console.log(google.timelineObjects[1].hasOwnProperty("activitySegment")); // false

If your objective to see what type of object you get.如果您的目标是查看您获得的 object 类型。 You can iterate over each object, see what the key of the object is and process the data depending on the key value.可以遍历每个object,看看object的key是什么,根据key值处理数据。 Something like this.像这样的东西。

Object.entries(google).forEach(([key, value]) => {
   if(key==='activitySegment') {
     //process activeSegment here   
   }else {
     //process placeVisit here
   }
})

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

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