0

I've got an array that holds a mix of objects and arrays that contain objects with the same properties

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
]

Trying to take each individual object, look up the value of "output" and push that many more objects into a new array:

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

And getting error "cannot read property "output" of undefined" for that plantObj[i] reference... without suggesting others methods and solutions and such please and thanks (I know they're out there, this a school exercise!), am I missing something in either the syntax or the logic for accessing the value of the "output" property of those nested objects? The way I'm reading this, starting at the if statement, is "If plantItem is an array, set a variable i to 0, and as long as i is less than the value of the "output" property of the entry found at index position i of plantItem ...etc".

Rest of it executes fine.

Thanks!

**EDIT plantObj was a typo, fixed **

1
  • You have not defined plantObj anywhere, let me know what you are trying to achieve, I can help you with the code Commented Jul 25, 2020 at 6:32

4 Answers 4

1

You can flat array and then 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);

Sign up to request clarification or add additional context in comments.

2 Comments

He said he don't want a way to do it but why his way doesn't work.
flat() is a good resourse to access nested array and i think is helpfull to be aware of that
1

In the first for you need a for to loop through your array then another one to loop through output. In your second for you push plantObj wich doesn't exist.

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 Comment

He wants to iterate it 25 times if output is 25.
1

You have missed nested array looping. And instead of plantItem you have used as plantObj. I have added as the comment in the respective place.

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

Moreover I would to give a suggestion, You could move the following code part to a separate function to avoid code repetition.

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

Comments

0

This snippet will solve your problem ;)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.