how to get all the values of an object?

1
// practica de clasess 
class Restaurante{
    constructor(menu = {}, meseros = [], horarios = [] ){
        this.menu = menu
        this.meseros = meseros
        this.horarios = horarios
    }
    verMenu(){
        let menu = this.menu
        for(let i = 0; i< menu.length; i++){
            console.log('Plato: ${menu[i]}\n')
        }
    }
}

const victorFood = new Restaurante()

victorFood.menu = {
    'pastas' :[
        '4 quesos',
        'primavera'
    ]
}

How can I access all the properties of victorFood.menu? obtaining an ordered output, thanks in advance.

    
asked by Victor Lozada 07.05.2018 в 07:32
source

2 answers

1

A javascript object is made up of properties and values, like this: {propiedad:valor, propiedad2:valor2};

You can obtain the values of an object in the following way:      Object.values(objeto) That will give you an array with all the values

And in your code you can do it like this:

verMenu(){
    let menu = Object.values(this.menu);
    for(let i = 0; i<menu.length ; i++){
        console.log(menu[i])
    }
}

To obtain an array of all the properties, it is done like this:

Object.keys(objeto);
    
answered by 07.05.2018 / 07:43
source
0
// practica de clasess 
class Restaurante{
    constructor(menu = {}, meseros = [], horarios = [] ){
        this.menu = menu
        this.meseros = meseros
        this.horarios = horarios
    }
    verMenu(){
        let menuType = Object.keys(this.menu) //nombre de la propiedad        
        menuType.map((val, i, menuType) => {
            let nowMenu = this.menu[val]// nombre del menu en el momento de ejecucion de la funcion en map()
            console.log('${val} \n|')
            nowMenu.map((val, i, nowMenu)=> console.log('----${val}'))
        })
    }
}

const victorFood = new Restaurante()

victorFood.menu = {
    'pastas' :[
        '4 quesos',
        'primavera'
    ],
    'carnes' :[
        'Lomito',
        'Parrilla'
    ]
}
victorFood.verMenu()

This way I can do it.

    
answered by 08.05.2018 в 08:18