How to list objects in JavaScript [closed]

1

I'm using a object exp variable:

var obj = {
nome: "Marcos",
snome: "Bonado",
idade: "314",
email: "[email protected]",
tellW: "0123456789"
};

I want to list the names and contents of each object. Is there any way for me to do this?

    
asked by anonymous 30.10.2017 / 20:53

3 answers

1

To have no problem with incompatibility between browsers use for

var obj = {
nome: "Marcos",
snome: "Bonado",
idade: "314",
email: "[email protected]",
tellW: "0123456789"
};

for (var k in obj){
    if (obj.hasOwnProperty(k)) {
         console.log("Chave: " + k + ", Valor: " + obj[k]);
    }
}
    
30.10.2017 / 21:03
0

First you must create an array and store the objects. Then to list in the console the objects of the array you can use console.log (array). If you want to see the contents of each object in isolation you can make a for loop.

For example:

var objeto = {"nome":"Bruno"};
var objeto2 = {"nome": "teste"};

var array = [objeto, objeto2];
console.log(array);

for(var i=0; i < array.length; i++){
  console.log(array[i]);
}
    
30.10.2017 / 21:04
0

Try this:

function (data) {
    var keys = Object.keys(data);

    for (var i = 0; i < keys.length; i++) {

    }
}
    
30.10.2017 / 21:02