gpt4 book ai didi

javascript - 如果我使用 JQuery $.each() 迭代对象属性,是否有索引?

转载 作者:行者123 更新时间:2023-11-30 08:07:59 27 4
gpt4 key购买 nike

a = {b:'bb',c:'cc',d:'dd'};
$.each(a, function(index){
// here 'index' is a string variable containing the property name, ie 'b', 'c' or 'd'.
}

我想要属性的整数索引 - 这是否可以在不使用独立计数变量的情况下实现?

最佳答案

你可以这样做:

Object.keys( a ).forEach(function ( name, index ) {
var value = a[name];

name // the property name
value // the value of that property
index // the counter
});

这是 ES5 API,所以你需要 IE8 的 es5-shim。

关于javascript - 如果我使用 JQuery $.each() 迭代对象属性,是否有索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804362/

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