0

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.

3
  • Can you give an example of your desired strucutre Commented Dec 13, 2020 at 10:27
  • [[designer name, shoe name, price], [designer name, shoe name, price], ....] E.g., Commented Dec 13, 2020 at 10:28
  • if you think one of the answers were helpful mark it as accepted, the button below down-upvoting. Commented Feb 14, 2021 at 16:15

3 Answers 3

1

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

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

7 Comments

is that not what I am doing? I'm not sure what you are referencing to here: for(let j = 0; j < inventory[i].shoes.length; j++) {
no, you have only the length without a comparison with j.
ok I updated it with the j comparison..and it gave me a whole new error that is even longer
and which one ?
Expected $.length = 4 to equal 6. Expected $[0].length = 1 to equal 3. Expected $[0][0] = 'Brunello Cucinelli, tasselled black low-top lace-up, 1000' to equal 'Brunello Cucinelli'.....
|
0

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.

Comments

0

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

7 Comments

ok I corrected that but it threw a whole new error
which one throws it?
Expected $.length = 4 to equal 6. Expected $[0].length = 1 to equal 3. Expected $[0][0] = 'Brunello Cucinelli, tasselled black low-top lace-up, 1000' to equal 'Brunello Cucinelli'. ....
For what do you use this dollar syntax?
im not using that...that is the direct copy of the failed test is...I believe its an unexpected outcome error
|

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.