andy86
andy86

Reputation: 75

Looping through an array of objects to create a list array

I'm having trouble creating a list array from an array of objects. I'm not sure where I am going wrong. here is my problem:

Look up all the shoes across all the designers and list them out in an array of arrays with this format:

[[designer name, shoe name, price], [designer name, shoe name, price], ....] E.g.,

const currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];

function renderInventory(inventory) {
 let arr = [];
 
 for(let i = 0; i < inventory.length; i++) {
   for(let j = 0;j < inventory[i].shoes.length; j++) {
     arr.push([`${inventory[i].name}, ${inventory[i].shoes[j].name}, ${inventory[i].shoes[j].price}`])
   }
 }
 
  return arr;
}

I am receiving this error and I am not sure why?

Expected $[0].length = 1 to equal 3. Expected $[0][0] = 'Brunello Cucinelli, tasselled black low-top lace-up, 1000' to equal 'Brunello Cucinelli'. Expected $[0][1] = undefined to equal 'tasselled black low-top lace-up'. Expected $[0][2] = undefined to equal 1000. Expected $[1].length = 1 to equal 3. Expected $[1][0] = 'Brunello Cucinelli, tasselled green low-top lace-up, 1100' to equal 'Brunello Cucinelli'. Expected $[1][1] = undefined to equal 'tasselled green low-top lace-up'. Expected $[1][2] = undefined to equal 1100. Expected $[2].length = 1 to equal 3. Expected $[2][0] = 'Brunello Cucinelli, plain beige suede moccasin, 950' to equal 'Brunello Cucinelli'. Expected $[2][1] = undefined to equal 'plain beige suede moccasin'. Expected $[2][2] = undefined to equal 950. Expected $[3].length = 1 to equal 3. Expected $[3][0] = 'Brunello Cucinelli, plain olive suede moccasin, 1050' to equal 'Brunello Cucinelli'. Expected $[3][1] = undefined to equal 'plain olive suede moccasin'. Expected $[3][2] = undefined to equal 1050. Expected $[4].length = 1 to equal 3. Expected $[4][0] = 'Gucci, red leather laced sneakers, 800' to equal 'Gucci'. Expected $[4][1] = undefined to equal 'red leather laced sneakers'. Expected $[4][2] = undefined to equal 800. Expected $[5].length = 1 to equal 3. Expected $[5][0] = 'Gucci, black leather laced sneakers, 900' to equal 'Gucci'. Expected $[5][1] = undefined to equal 'black leather laced sneakers'. Expected $[5][2] = undefined to equal 900.

Also,

I thought that I could possible use .map() and .reduce() to solve this problem as well but I'm not sure where to start.

Upvotes: 0

Views: 109

Answers (3)

Aalexander
Aalexander

Reputation: 5004

In this line you have missed a comparison

for(let j = 0; inventory[i].shoes.length; j++) {

Write this

   for(let j = 0;j < inventory[i].shoes.length; j++) {

const currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];

function renderInventory(inventory) {
 let arr = [];
 
 for(let i = 0; i < inventory.length; i++) {
   for(let j = 0; j < inventory[i].shoes.length; j++) {
     arr.push([inventory[i].name, inventory[i].shoes[j].name, inventory[i].shoes[j].price])
   }
 }
 
  return arr;
}

console.log(renderInventory(currentInventory));

Upvotes: 0

Rodrigo V
Rodrigo V

Reputation: 514

You need to write this way instead:

for(let i = 0; i < inventory.length-1; i++) {
    // code here
}

The reason for the error is that the length property does not necessarily indicate the number of defined values in the array.

let array = ['apple', 'banana'];

console.log(array.length);
// Expected result: 2

console.log(array[2]);
// Expected result: undefined

This is why the last item was undefined.

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386604

You need to check for the length

for(let j = 0; j < inventory[i].shoes.length; j++) {
               ^^^^

const currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];

function renderInventory(inventory) {
 let arr = [];
 
 for(let i = 0; i < inventory.length; i++) {
   for(let j = 0; j < inventory[i].shoes.length; j++) {
     arr.push([`${inventory[i].name}, ${inventory[i].shoes[j].name}, ${inventory[i].shoes[j].price}`])
   }
 }
 
  return arr;
}

console.log(renderInventory(currentInventory));

A more easier way is to use for ... of statement by taking the objects directly.

function renderInventory(inventory) {
    let arr = [];

    for (const { name, shoes } of inventory)
        for (const shoe of shoes)
            arr.push([`${name}, ${shoe.name}, ${shoe.price}`]);

    return arr;
}

const
    currentInventory = [{ name: 'Brunello Cucinelli', shoes: [{ name: 'tasselled black low-top lace-up', price: 1000 }, { name: 'tasselled green low-top lace-up', price: 1100 }, { name: 'plain beige suede moccasin', price: 950 }, { name: 'plain olive suede moccasin', price: 1050 }] }, { name: 'Gucci', shoes: [{ name: 'red leather laced sneakers', price: 800 }, { name: 'black leather laced sneakers', price: 900 }] }];


console.log(renderInventory(currentInventory));

Upvotes: 1

Related Questions