var work={
"title": ["test1", "test2"]
}
for(var x in work)
console.log(work.title[x]);
为什么它在控制台中显示未定义
答案 0 :(得分:0)
var work = {
"title": ["internship", "test"],
"employer": ["sdc", "test"],
"date": [2017, 2018],
"location": ["Bouira", "Alger"],
"description": ["Réalisation logiciel gestion park", "test"]
}
// for...in
for (var key in work) {
var value = work[key];
console.log(value, '\n');
// for...of
for (var item of value) {
console.log(item, '\n\n');
}
}
答案 1 :(得分:0)
您可能希望单独列出test1和test2,在这种情况下,以下内容可行:
for (var x in work["title"]) console.log(work["title"][x]);
答案 2 :(得分:0)
您的title
工作正常,因此for(var x in work)
会重复title
,因此会打印["test1", "test2"]
var work={
"title": ["test1", "test2"]
}
for(var x in work) {
console.log(work[x]);
}
答案 3 :(得分:0)
如果您需要打印标题,您应该:
var work = {
"title": ["test1", "test2"]
}
for (var title of work.title) {
console.log(title);
}
或者:
var work = {
"title": ["test1", "test2"]
}
work.title.forEach(function(title) {
console.log(title);
});
我猜这些是更简洁的javascript方式,在大多数情况下你并不需要使用数组索引。
答案 4 :(得分:0)
没有人提到for (... of ...)
给我们的价值而不是指数
let work = {'title': ['test1', 'test2']}
for (let x of work.title) {
console.log(x);
}