Pns*_*ghy -1 javascript arrays each jquery
我有一个数组,我需要使用每个项目,但是forEach和$ .each在这里不适合数组(jsfiddle)
var person = new Array();
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46;
jQuery.each(person, function(index, item) {
alert(index + " : " + item);
});
Run Code Online (Sandbox Code Playgroud)
那是一个关联数组,实际上只是一个对象。要枚举某个对象的键值对obj,请使用for循环:
for (let key in obj) {
console.log(key, ":", obj[key])
}
Run Code Online (Sandbox Code Playgroud)