如何将对象数组中的特定键作为 for 循环内条件的目标?
How do I target a specific key in an array of objects for a conditional inside a for loop?
JS 新手!尝试为我的对象中的一个键指定一个语句,该对象再次嵌套在一个数组中。在 Slowbro 旁边 - 应显示消息:“哇,这是一只大口袋妖怪”,但这不起作用。我试图在我的 for 循环中为所有口袋妖怪写一个一般条件,但也指定“哇,这是一个大口袋妖怪”消息应该只出现在 Slowbro 而不是任何满足 height>1.5 标准的人。我错过了什么?
//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
{name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
{name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
{name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
];
//Loop lists each Pokemon in array by assigning name and height keys
for (let i=0; i < pokemonList.length; i++)
{ document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>')
}
for (let i=0; i < pokemonList[i].length; i++) {
if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') {
console.log('- Wow, this is a big Pokemon!');
} else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) {
console.log('- This is an average size Pokemon.');
} else {
console.log('- This is a small Pokemon.')
}
}
在代码段的最后一个 for
循环中,您使用的不是数组的长度,而是第一个元素的长度:
for (let i=0; i < pokemonList[a].length; i++)
您可能需要数组本身的长度:
for (let i=0; i < pokemonList.length; i++)
相反。
//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
{name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
{name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
{name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
];
//Loop lists each Pokemon in array by assigning name and height keys
for (let i=0; i < pokemonList.length; i++)
{ document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>')
}
for (let i=0; i < pokemonList.length; i++) {
if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') {
console.log('- Wow, this is a big Pokemon!');
} else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) {
console.log('- This is an average size Pokemon.');
} else {
console.log('- This is a small Pokemon.')
}
}
试试这个
//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
{name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
{name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
{name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
];
const html = pokemonList.map(({name, height}) => {
return `${name} (height: ${height})`
}).join('<br>')
document.write(html)
pokemonList.forEach(({name, height}) => {
if( height > 1.5){
console.log('- Wow, this is a big Pokemon!')
}else if(height > 0.5){
console.log('- This is an average size Pokemon.');
} else {
console.log('- This is a small Pokemon.')
}
})
JS 新手!尝试为我的对象中的一个键指定一个语句,该对象再次嵌套在一个数组中。在 Slowbro 旁边 - 应显示消息:“哇,这是一只大口袋妖怪”,但这不起作用。我试图在我的 for 循环中为所有口袋妖怪写一个一般条件,但也指定“哇,这是一个大口袋妖怪”消息应该只出现在 Slowbro 而不是任何满足 height>1.5 标准的人。我错过了什么?
//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
{name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
{name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
{name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
];
//Loop lists each Pokemon in array by assigning name and height keys
for (let i=0; i < pokemonList.length; i++)
{ document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>')
}
for (let i=0; i < pokemonList[i].length; i++) {
if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') {
console.log('- Wow, this is a big Pokemon!');
} else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) {
console.log('- This is an average size Pokemon.');
} else {
console.log('- This is a small Pokemon.')
}
}
在代码段的最后一个 for
循环中,您使用的不是数组的长度,而是第一个元素的长度:
for (let i=0; i < pokemonList[a].length; i++)
您可能需要数组本身的长度:
for (let i=0; i < pokemonList.length; i++)
相反。
//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
{name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
{name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
{name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
];
//Loop lists each Pokemon in array by assigning name and height keys
for (let i=0; i < pokemonList.length; i++)
{ document.write(pokemonList[i].name+ ' (height: ' + pokemonList[i].height + ') ' + '<br>')
}
for (let i=0; i < pokemonList.length; i++) {
if (pokemonList[i].height > 1.5 && pokemonList[i].name === 'Slowbro') {
console.log('- Wow, this is a big Pokemon!');
} else if (pokemonList[i].height > 0.5 && pokemonList[i].height < 1.5) {
console.log('- This is an average size Pokemon.');
} else {
console.log('- This is a small Pokemon.')
}
}
试试这个
//Array contains Pokemon data to display in application
//Each object represents one Pokemon with respective properties
let pokemonList = [
{name:'Pikachu', height: 0.4, type:['electric','ground','steel','flying']},
{name:'Charmeleon', height: 1.1, type:['ground','rock', 'water']},
{name: 'Slowbro' ,height: 1.6, type:['electric', 'grass','dragon','ghost','bug']}
];
const html = pokemonList.map(({name, height}) => {
return `${name} (height: ${height})`
}).join('<br>')
document.write(html)
pokemonList.forEach(({name, height}) => {
if( height > 1.5){
console.log('- Wow, this is a big Pokemon!')
}else if(height > 0.5){
console.log('- This is an average size Pokemon.');
} else {
console.log('- This is a small Pokemon.')
}
})