查找json对象中的元素数

时间:2021-12-20 03:35:49

I can't figure out how to find the number of elements contained in a JSON object with JavaScript. This is how my object is structured:

我无法弄清楚如何使用JavaScript查找JSON对象中包含的元素数量。这就是我的对象的结构:

var shows = [];
//make http call here to api url and store in variable called 'data'
for (int i = 0; i < data.length; i++) {
  var element = {
    "location": {
      "latitude": data[j].venue.latitude,
      "longitude": data[j].venue.longitude
    },
    "artist": data[j].artists[0].name,
    "venue_name": data[j].venue.name,
    "date": data[j].datetime
  };
  shows.push(element);
};

So I can access the elements easily by using shows[i].location or whatever key I'd like access. However, all the question/answers that cover this topic seem to recommend Object.keys(shows).length to find the number of elements (I've also had trouble getting a for-each loop to work for this object of elements).

所以我可以使用shows [i] .location或我想访问的任何键轻松访问元素。但是,涵盖此主题的所有问题/答案似乎都建议使用Object.keys(shows).length来查找元素的数量(我也很难找到for-each循环来为这个元素对象工作)。

However, this doesn't work in my case (I'm assuming because 'shows' doesn't have any direct keys) so I was just wondering if there is any other way to access the number of elements (each with 4 key : value pairs) that are inside of "shows"? Or is there just another way to iterate over every element? Or if that is not possible is there another way I could structure my object to make it easier to find the number of elements? Any help is much appreciated!

但是,这在我的情况下不起作用(我假设因为'节目'没有任何直接键)所以我只是想知道是否有任何其他方式来访问元素的数量(每个有4个键:价值对)在“节目”里面?或者是否有另一种迭代每个元素的方法?或者,如果这是不可能的,还有另一种方法可以构建我的对象,以便更容易找到元素的数量?任何帮助深表感谢!

2 个解决方案

#1


0  

Given

特定

[A]ny other way to access the number of elements (each with 4 key : value pairs) that are inside of "shows".

[A] ny其他方式来访问“shows”中的元素数量(每个元素有4个键:值对)。

The "number of elements" you are speaking of is the length of the array that you are pushing objects onto. The objects pushed each have four key-value pairs.

您所说的“元素数量”是您将对象推入的数组的长度。每个推送的对象都有四个键值对。

You would determine the number of elements (length of the array) using (or in an array):

您将使用(或在数组中)确定元素的数量(数组的长度):

var lengthOfShows = shows.length;
for (var i = 0; i < shows.length; i++) {
  var currentShow = shows[i];
  // references
  // shows[i].location
}

In your code ...

在你的代码中......

for (int i = 0; i < data.length; i++) {

Should be ...

应该 ...

for (var j = 0; j < data.length; j++) {

Since you are referencing data[j] inside the for-loop.

因为你在for循环中引用数据[j]。

#2


0  

From your code, shows is an array. Arrays have a length property.
shows.length

从您的代码中,show是一个数组。数组有长度属性。 shows.length

is probably what you need. Looks like it would be the same as data.length and that data is probably also an array.

可能就是你需要的。看起来它与data.length相同,并且该数据可能也是一个数组。

#1


0  

Given

特定

[A]ny other way to access the number of elements (each with 4 key : value pairs) that are inside of "shows".

[A] ny其他方式来访问“shows”中的元素数量(每个元素有4个键:值对)。

The "number of elements" you are speaking of is the length of the array that you are pushing objects onto. The objects pushed each have four key-value pairs.

您所说的“元素数量”是您将对象推入的数组的长度。每个推送的对象都有四个键值对。

You would determine the number of elements (length of the array) using (or in an array):

您将使用(或在数组中)确定元素的数量(数组的长度):

var lengthOfShows = shows.length;
for (var i = 0; i < shows.length; i++) {
  var currentShow = shows[i];
  // references
  // shows[i].location
}

In your code ...

在你的代码中......

for (int i = 0; i < data.length; i++) {

Should be ...

应该 ...

for (var j = 0; j < data.length; j++) {

Since you are referencing data[j] inside the for-loop.

因为你在for循环中引用数据[j]。

#2


0  

From your code, shows is an array. Arrays have a length property.
shows.length

从您的代码中,show是一个数组。数组有长度属性。 shows.length

is probably what you need. Looks like it would be the same as data.length and that data is probably also an array.

可能就是你需要的。看起来它与data.length相同,并且该数据可能也是一个数组。