gpt4 book ai didi

Javascript 循环遍历对象数组

转载 作者:行者123 更新时间:2023-12-03 06:21:32 44 4
gpt4 key购买 nike

我试图循环遍历一个数组,它一直显示 length0

数组截图:

screenshot

我已经尝试过for(key in results)...for( var i = 0; i < length...但两者都不在循环内运行任何内容:console.log(results[i]); // Or key if it's a key in loop

我确信这是一个菜鸟错误,有人能发现吗?

我正在使用的代码:

    var store = new Lawnchair({name: 'testing'}, function (store) {

// Create an object
var me = {key: 'Jordy', age: 19, date_of_birth: "1233-09-06"};
var mee = {key: 'dude', age: 17, date_of_birth: "2222-09-06"};
var meee = {key: 'gast', age: 8, date_of_birth: "5555-09-06"};

// Save it
store.save(me);
store.save(mee);
store.save(meee);

// Access it later... Yes even after a page refresh!
store.where('record.age < 20', function (records) {
var html = "";
var list = document.getElementById('people');
// for (var i = 0; i < records.length; i++) {
// var record = records[i];
// html += "<li>" + record.key + " is " + record.age + " years old and was born on " + record.date_of_birth + "</li>";
// }

for(var key in records){
console.log("SD");
}

console.log(records);
list.innerHTML = html;

});
});

当我 console.log(records)如上所示,结果是屏幕截图。当我循环它时,没有任何反应,因为 records.length === 0

编辑:

store.where('record.age < 20', function(records){
console.log(records); // Array with property length 3
console.log(records.length); // 0
});

第一个console.log显示数组 records.length === 3但第二个console.log显示0 。怎么办?

最佳答案

如果不提供代码,很难说您是否犯了菜鸟错误!我发现您使用的是 length 而不是 arrayName.length?我不知道你的数组的名称...

for (var i = 0; i < array.length;i++){
//logic here
};

关于Javascript 循环遍历对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38847977/

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