簡體   English   中英

嘗試訪問嵌套在數組中的 object 的屬性時未定義的類型

[英]Typeof undefined when trying to access property of object nested in array

我有一個包含對象混合的數組和包含具有相同屬性的對象的 arrays

plantsArray = [
{type: "wheat", height: 20, output: 25},
{type: "soybeans", height: 30, output: 29},
[{type: "corn", height: 34, output: 9}, {type: "wheat", height: 20, output: 25}],
{type: "wheat", height: 20, output: 25}, 
..etc
]

嘗試獲取每個單獨的 object,查找“輸出”的值並將更多對象推送到新數組中:

const harvestOutput = []

for (const plantItem of plantsArray) {

        if (Array.isArray(plantItem) ) {
                for (let i = 0; i < plantItem[i].output; i++) {
                    harvestOutput.push(plantItem[i])
            }
        } 
        else

        for(let k = 0; k < plantItem.output; k++) {
            harvestOutput.push(plantItem)
        }
    }

並且得到錯誤“無法讀取該plantObj[i]參考的未定義屬性“輸出”......沒有建議其他方法和解決方案等請和感謝(我知道他們在那里,這是一個學校練習,),我在訪問這些嵌套對象的“輸出”屬性的值的語法或邏輯中是否遺漏了什么? 我從 if 語句開始閱讀本文的方式是“如果 plantItem 是一個數組,則將變量 i 設置為 0,並且只要 i 小於在索引 position i of plantItem...etc"。

它的 Rest 執行良好。

謝謝!

**編輯plantObj是一個錯字,已修復**

在第一個 for 中,您需要一個 for 循環遍歷您的陣列,然后另一個循環遍歷 output。 在你第二次為你推送不存在的plantObj。

 plantsArray = [ {type: "wheat", height: 20, output: 25}, {type: "soybeans", height: 30, output: 29}, [{type: "corn", height: 34, output: 9}, {type: "wheat", height: 20, output: 25}], {type: "wheat", height: 20, output: 25} ] const harvestOutput = []; for (const plantItem of plantsArray) { if (Array.isArray(plantItem) ) { for (let i = 0; i < plantItem.length; i++) for (let j = 0; j <= plantItem[i].output; j++) { harvestOutput.push({...plantItem[i]}); // the {...} is to destructuring the object and make a copy } } else for (let j = 0; j <= plantItem.output; j++) { harvestOutput.push({...plantItem}); } } console.log(harvestOutput);

你可以先平面陣列然后map

 let plantsArray = [ {type: "wheat", height: 20, output: 25}, {type: "soybeans", height: 30, output: 29}, [{type: "corn", height: 34, output: 9}, {type: "wheat", height: 20, output: 25}], {type: "wheat", height: 20, output: 25}]; let result = plantsArray.flat(1).map(x => x.output); console.log(result);

您錯過了嵌套數組循環。 而不是 plantItem,您已將其用作 plantObj。 我已在相應位置添加為評論。

 const plantsArray = [{ type: "wheat", height: 20, output: 25 }, { type: "soybeans", height: 30, output: 29 }, [{ type: "corn", height: 34, output: 9 }, { type: "wheat", height: 20, output: 25 }], { type: "wheat", height: 20, output: 25 } ] const harvestOutput = [] for (const plantItem of plantsArray) { if (Array.isArray(plantItem)) { //You have missed this looping for (let i = 0; i < plantItem.length; i++) { for (let k = 0; k < plantItem[i].output; k++) { harvestOutput.push(plantItem[i]) } } } else //Instead of plantItem you have used plantObj here so it is undefined. for (let k = 0; k < plantItem.output; k++) { harvestOutput.push(plantItem) } } console.log(harvestOutput);

此外,我想提出一個建議,您可以將以下代碼部分移動到單獨的 function 以避免代碼重復。

 for (let k = 0; k < plantItem[i].output; k++) {
     harvestOutput.push(plantItem[i])
 }

此代碼段將解決您的問題;)

 const plantsArray = [ { type: 'wheat', height: 20, output: 25 }, { type: 'soybeans', height: 30, output: 29 }, [{ type: 'corn', height: 34, output: 9 }, { type: 'wheat', height: 20, output: 25 }], { type: 'wheat', height: 20, output: 25 } ]; let harvestOutput = []; plantsArray.flat(1).forEach(plantItem => { const harvestedItems = new Array(plantItem.output).fill(plantItem); harvestOutput = harvestOutput.concat(harvestedItems); }); console.log(harvestOutput);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM