gpt4 book ai didi

jquery - 使用 jQuery 或 javascript 获取 JSON 数组的长度

转载 作者:行者123 更新时间:2023-12-01 00:51:32 25 4
gpt4 key购买 nike

我知道有很多类似的问题。但它们中的任何一个都不适合我。

我有一个 json 数组,其整个结构如下:

enter image description here

我想要得到的数组是这样的:

enter image description here

该 json 数组的结构是:

enter image description here

我想获取该 json 数组的长度。在上图中,它是4。到目前为止我尝试过的是:

console.log( $( data.studentMockBeanMap ).size() );
console.log( $(data.studentMockBeanMap ).length );.

两者都返回1

我也尝试这个:

var x = JSON.stringify(data.studentMockBeanMap);
console.log( x.length );

它返回2257,IMO它还返回所有json对象的总和。

我怎样才能只使用上面方框中的图像的尺寸?

最佳答案

这与Object.keys(obj).length的想法相同,但没有浏览器兼容性问题(我认为)。

关于:

var obj = {
yo: {
a: 'foo',
b: 'bar'
}
hi: {
c: 'hello',
d: 'world',
e: 'json'
}
}

var arr = [], len;

for(key in obj) {
arr.push(key);
}

len = arr.length;

console.log(len) //2

或者

var arr = [], len;

for(key in obj.hi) {
arr.push(key);
}

len = arr.length;

console.log(len) //3

或者,根据您的情况

var arr = [], len;

for(key in studentMockBeanMap) {
arr.push(key);
}

len = arr.length;

console.log(len); //4

关于jquery - 使用 jQuery 或 javascript 获取 JSON 数组的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20811318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com