尝试访问嵌套在数组中的对象的属性时,typeof未定义

时间:2020-07-25 06:28:24

标签: javascript arrays for-loop

我有一个数组,其中包含对象和包含具有相同属性的对象的数组的组合

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
]

尝试获取每个对象,查找“输出”的值,然后将更多对象推入新数组:

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小于在以下位置找到的条目的“输出”属性的值plantItem ... etc的索引位置i”。

其余的执行正常。

谢谢!

**编辑plantObj是一个错字,已修复**

4 个答案:

答案 0 :(得分:1)

首先,您需要一个for循环遍历数组,然后另一个需要循环遍历输出。在您的第二次推送中,不存在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);

答案 1 :(得分:1)

您可以先对数组进行平面排列,然后进行映射

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);

答案 2 :(得分:1)

您错过了嵌套数组循环。并且已代替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);

我还要提出一个建议, 您可以将以下代码部分移至单独的函数,以避免代码重复。

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

答案 3 :(得分:0)

此代码段将解决您的问题;)

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);