2 回答

TA貢獻1799條經驗 獲得超6個贊
無法直接運行 for 循環的原因是testOne數組嵌套在對象內。
此外,數組本身還有進一步的嵌套。因此,我認為處理這個問題的最佳方法是在對象上使用點并檢索 testOne數組,然后在該數組上使用 for 循環或使用 Array.prototype.forEach( ) 函數。
我在下面提供了兩種方法,您可以選擇一種適合您的方法。
const test = {
testOne: [
{
situation: {
status: 'reproved',
},
},
{
situation: {
status: 'rejected',
},
},
{
situation: {
status: 'approved',
},
},
],
};
//retrieving testOne from test object via dot syntax
const testOne = test.testOne;
//Approach 1 - For Loop - on testOne array
console.log('Approach - 1');
for (let i = 0; i < testOne.length; i++) {
console.log(testOne[i].situation.status);
}
//Approach 2 - Array.prototype.forEach( ) - on testOne array
console.log('Approach - 2');
testOne.forEach(function(obj){
console.log(obj.situation.status)
});

TA貢獻1815條經驗 獲得超10個贊
添加回答
舉報