Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How could I resolve this javascript matrix?

I have a fashion catalog, an inventory of items. Each designer has a lineup of items. Each item has a name and a price.

I have to write a function that will receive as a parameter an array. The function has to access all the items across each designer and return a matrix like:

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

But I'm stuck with this: ((this is my return))

[
  'Brunello Cucinelli',
  [ 'tasselled black low-top lace-up', 1000 ],
  'Brunello Cucinelli',
  [ 'tasselled green low-top lace-up', 1100 ],
  'Brunello Cucinelli',
  [ 'plain beige suede moccasin', 950 ],
  'Brunello Cucinelli',
  [ 'plain olive suede moccasin', 1050 ],
  'Gucci',
  [ 'red leather laced sneakers', 800 ],
  'Gucci',
  [ 'black leather laced sneakers', 900 ]
]

var currentInventory = [  //array with data
    {
        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 matrix = []

  let props = []

    for(let i=0; i < inventory.length; i++){ //loop 1 inventory
      matrix.push(inventory[i].name)

        for(let n=0; n < inventory[i].shoes.length; n++){ //loop 2 shoes()
          
                    props.push(matrix[i])
                    props.push(Object.values(inventory[i].shoes[n]))

        }  //end loop 2

    } //end loop 1
  
   

return props;
}

renderInventory(currentInventory)
 

Any help please?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a nested map() call and then flatten the result (here using flatMap() which combines the operations). This example also uses destructuring with aliases.

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 }] }];

const result = currentInventory
  .flatMap(({ name: designer, shoes }) =>
    shoes.map(({ name: shoe, price }) => [designer, shoe, price])
  )

console.log(result)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!