如何显示对象数组的每个元素的长度?

时间:2019-11-14 17:36:05

标签: javascript

下面的JOURNALAL数组包含9个对象。每个对象都包含一个数组。例如,第一个对象包含一个数组,其中包含3个元素,分别是“胡萝卜”,“运动”,“周末”。我的目标是编写代码来显示每个对象中包含的每个数组的长度。 感谢您的输入。

var JOURNAL = [
  {"events1":["carrot","exercise","weekend"],"squirrel":false},
  {"events2":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
  {"events3":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
  {"events4":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false},
  {"events5":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false},
  {"events6":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false},
  {"events7":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false},
  {"events8":["bread","beer","brushed teeth","cycling","work"],"squirrel":false},
  {"events9":["cauliflower","brushed teeth","work"],"squirrel":false}
];

for(let i = 0; i < JOURNAL.length; i++){
  let entry = JOURNAL[i];
  console.log(JOURNAL[i].length);
}

如果我的日记在下面,那么我不会有问题

		var JOURNAL = [
		  {"events":["carrot","exercise","weekend"],"squirrel":false},
		  {"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
		  {"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
		  {"events":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false},
		  {"events":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false},
		  {"events":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false},
		  {"events":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false},
		  {"events":["bread","beer","brushed teeth","cycling","work"],"squirrel":false},
		  {"events":["cauliflower","brushed teeth","work"],"squirrel":false}
		];

		for(let i=0; i<JOURNAL.length; i++){
			let entry = JOURNAL[i];
			console.log(JOURNAL[i].events.length);
		}

5 个答案:

答案 0 :(得分:0)

这是您需要的吗?

def longestFalse(L):
endindex = 0
maxcount=0
counter=0
for i in range(len(L)):
    if L[i] == False:
        counter += 1
    elif L[i] == True:
        if counter>maxcount:
            maxcount = counter
            endindex = i
            counter = 0
    elif i == len(L) - 1 :
        if L[i-1] == L[i]:
            counter += 1
            maxcount = count
            endindex = i
return endindex-maxcount,endindex-1

答案 1 :(得分:0)

我不确定这是否要实现,但是如果您使用JOURNAL循环遍历数组(for),则可以使用{ {1}}。然后,您将拥有一个像这样的对象:

JOURNAL[i]

要访问{ "events1": [ "carrot", "exercise", "weekend" ], "squirrel":false } 中数组的长度,可以只使用events1。如果单词JOURNAL[i].events1.length后面的数字始终增加一个并以events开头,则可以这样操作:

1

JOURNAL[i]['events' + (i + 1)].length


最好使用其他数据结构,其中键var JOURNAL = [ {"events1":["carrot","exercise","weekend"],"squirrel":false}, {"events2":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false}, {"events3":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false}, {"events4":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false}, {"events5":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false}, {"events6":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false}, {"events7":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false}, {"events8":["bread","beer","brushed teeth","cycling","work"],"squirrel":false}, {"events9":["cauliflower","brushed teeth","work"],"squirrel":false} ]; for(let i = 0; i < JOURNAL.length; i++) { let entry = JOURNAL[i]; let number = i + 1; console.log(entry['events' + number].length); } console.log('---'); // or with for .. of let counter = 0; for (let entry of JOURNAL) { counter++; console.log(entry['events' + counter].length); }不会增加值。你能做这样的事吗?

events

答案 2 :(得分:0)

尝试使用Object.entries()功能。

var JOURNAL = [
  {"events1":["carrot","exercise","weekend"],"squirrel":false},
  {"events2":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
  {"events3":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
  {"events4":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false},
  {"events5":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false},
  {"events6":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false},
  {"events7":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false},
  {"events8":["bread","beer","brushed teeth","cycling","work"],"squirrel":false},
  {"events9":["cauliflower","brushed teeth","work"],"squirrel":false}
];
JOURNAL.map(item => {
    for (let [key, value] of Object.entries(item)) {
        if (Array.isArray(value)) {
            console.log(`{$key} length is ${value.length}.`);
        } 
    }
}

官方指南:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

答案 3 :(得分:0)

根据您的陈述,即如果属性名称一致,则不会有问题,您可以按照前两个答案的建议进行操作:

console.log(JOURNAL[i]["events" + (i + 1)].length);

问题是,这很棘手。一旦属性名称与数组中的位置不一致,它就会失败。

一种更可靠的解决方案是找到与eventsN匹配的属性,其中N是任意数字系列:

const prop = Object.keys(entry).find(key => /^events\d+$/.test(key));
console.log(prop ? entry[prop].length : -1); // Where -1 is "didn't find it"

实时示例:

var JOURNAL = [
  {"events1":["carrot","exercise","weekend"],"squirrel":false},
  {"events2":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
  {"events3":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
  {"events4":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false},
  {"events5":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false},
  {"events6":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false},
  {"events7":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false},
  {"events8":["bread","beer","brushed teeth","cycling","work"],"squirrel":false},
  {"events9":["cauliflower","brushed teeth","work"],"squirrel":false}
];

for(let i = 0; i < JOURNAL.length; i++){
  let entry = JOURNAL[i];
  const prop = Object.keys(entry).find(key => /^events\d+$/.test(key));
  console.log(prop ? entry[prop].length : -1); // Where -1 is "didn't find it"
}


旁注:如果愿意,可以使用for-of循环遍历JOURNAL

for (const entry of JOURNAL) {
  const prop = Object.keys(entry).find(key => /^events\d+$/.test(key));
  console.log(prop ? entry[prop].length : -1); // Where -1 is "didn't find it"
}

答案 4 :(得分:0)

您可以简单地遍历数组并获取值,如果是数组,请检查其长度。

for(const elem of JOURNAL) {
 for(const val of Object.values(elem)){
    if(Array.isArray(val)){
        console.log('size - ', val.length);
    }
 }

}