Why does my array return an undefined

0

I have this code, but I want to access a attribute of an object that is within an array , but is returning undefined

obj1 = {
    nome : 'name',
    array : [],
    funcao : function(element){this.array.push(element)},
    getArray : function(){return this.array},
    getNome : function(){this.array.forEach(element =>element.nome) }
}

obj2 = {
    nome: 'name2'
}

obj1.funcao(obj2)
console.log(obj1.getArray())
console.log(obj1.getNome())
    
asked by anonymous 10.04.2018 / 23:37

1 answer

1

If you want to return a new array with the name property, try using the map method, as follows:

obj1 = {
    nome : 'name',
    array : [],
    funcao : function(element){this.array.push(element)},
    getArray : function(){return this.array;},
    getNome : function(){return this.array.map(element =>element.nome) }
}

obj2 = {
    nome: 'name2'
}

obj1.funcao(obj2)
console.log(obj1.getArray())
console.log(obj1.getNome())
    
10.04.2018 / 23:56