How to Do This in Javascript (Listing Products by Category, According to Database)

Asked

Viewed 211 times

-4

I’m making an application to order is my TCC and I’m having difficulty.

inserir a descrição da imagem aqui

  • 2

    That what? In the title you ask in Javascript, in the body of the question in PHP, but in no time you say what you really want to do. Please read the [Ask] guide and try to improve your question.

  • I’m wanting in Javascript

  • This image shows the part you’ve already done? Where exactly are you having difficulties?

  • So I just did the design part and I want the products to be separated by categories like in the image above.

1 answer

1

Hello, you can use reduce to create a new array grouped by categories.

See the example below:

    var dados = [
      {
        nome: 'cerveja',
        categoria: 'bebida'
      },
      {
       nome: 'suco',
       categoria: 'bebida'
      },
      {
       nome: 'joelho',
       categoria: 'salgado'
      },
      {
       nome: 'pastel',
       categoria: 'salgado'
      }
    ]

    var novoArray = dados.reduce(function(prev,curr){
      prev[curr.categoria] = prev[curr.categoria] || [];
      prev[curr.categoria].push(curr);
      return prev;
    },{});

    console.log(novoArray)
  • But how to make the database interact with the array?

  • Well, I believe you have to create a "service" in the back end that makes this bridge. here, maybe he’ll help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.