I have the following array:
var array = {
x: "Primeiro Valor",
y: "Segundo Valor",
z: "Terceiro Valor"
};
I would like to access the values, but I do not know the indexes x,y,z
, can you extract those values?
I have the following array:
var array = {
x: "Primeiro Valor",
y: "Segundo Valor",
z: "Terceiro Valor"
};
I would like to access the values, but I do not know the indexes x,y,z
, can you extract those values?
This is an Object , to read its properties do:
var array = {
x: "Primeiro Valor",
y: "Segundo Valor",
z: "Terceiro Valor"
};
for (var prop in array) {
console.log("propriedade: " + prop + " valor: " + array[prop])
}
Actually what you have there is not an array , but an object .
But it is possible to iterate through its properties, using Object.keys()
, which converts the object into an array :
Object.keys(seuObjeto).forEach(function(key,index) {
console.log(seuObjeto[key]);
});
Reference Objects.keys () - Mozilla Developer Network