How to list objects in Javascript

Asked

Viewed 1,008 times

1

I’m using a variable object exp:

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

I want to list the names and content of each object. Is there any way I can do this?

3 answers

1


For no problem with browser incompatibility use the 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]);
    }
}

0

Try it like this:

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

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

    }
}

0

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

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

Browser other questions tagged

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