gpt4 book ai didi

javascript - Object.keys 及其值的数组循环

转载 作者:行者123 更新时间:2023-12-02 14:55:21 24 4
gpt4 key购买 nike

我试图显示一个数组循环,其中有一个日期及其值。我能够循环数组的 Objects.keys 但内部循环需要单独显示键数组。这是一个简单数组的任务,但我无法弄清楚。以下是我的代码。

var arr = {
'2016-03-06' : ['1', '2','3','4','5'], // 6th march 2016
'2016-03-07' : ['6','7','8','9','10','11'], // 7th march 2016
'2016-03-08' : ['2','3','4','5'], // 8th march 2016
'2016-03-09' : ['6','7','8','9','10','11'], // 9th march 2016
'2016-03-10' : ['1', '2','3'], // 10th march 2016
'2016-03-11' : ['6','7','8','9','10','11'], // 11th march 2016
};

var a=0;
var b=0;
ab = Object.keys(arr).length;
bc = Object.keys(arr)[b].length;
console.log(ab);
console.log(bc);

for(a=0; a < ab; a++){
$('.result .array').append('<li data-date="'+Object.keys(arr)[a]+'">'+ Object.keys(arr)[a] + '</li>');
for(b=0; b < Object.keys(arr).length; b++){
$('.result .array').append('<li data-date='+ Object.keys(arr)[a] +'>1</li>');
}
}

最佳答案

我相信您正在寻找这样的输出:

var arr = {
'2016-03-06' : ['1', '2','3','4','5'], // 6th march 2016
'2016-03-07' : ['6','7','8','9','10','11'], // 7th march 2016
'2016-03-08' : ['2','3','4','5'], // 8th march 2016
'2016-03-09' : ['6','7','8','9','10','11'], // 9th march 2016
'2016-03-10' : ['1', '2','3'], // 10th march 2016
'2016-03-11' : ['6','7','8','9','10','11'], // 11th march 2016
};

for (var key in arr) {

$('.result .array').append('<li data-date="'+key+'">'+ key + '</li>');

var associatedData = arr[key];
if($.isArray(associatedData)){
for (var i = 0; i < associatedData.length; i++) {
$('.result .array').append('<li data-date='+ associatedData[i] +'>'+associatedData[i]+'</li>');
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="result">
<div class="array">
</div>
</div>

关于javascript - Object.keys 及其值的数组循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35861371/

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