JS新手!試圖為我的物件中的一個鍵指定一個陳述句,該物件再次嵌套在一個陣列中。在 Slowbro 旁邊 - 應該顯示訊息:“哇,這是一個大口袋妖怪”,但這不起作用。我試圖在我的 for 回圈中為所有口袋妖怪寫一個一般條件,但也指定“哇,這是一個大口袋妖怪”訊息應該只出現在 Slowbro 而不是任何滿足高度> 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.')
}
}
uj5u.com熱心網友回復:
試試這個
//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.')
}
})
uj5u.com熱心網友回復:
在代碼片段的最后一個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.')
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460402.html
標籤:javascript for循环 if 语句 条件语句
下一篇:將for回圈中的值粘貼到資料框R
