How to get all the values of an object?

4
const obj = [{
nombre: "Nombre...",
apellidos: "Coo..",
edad: 22
}]

console.log(Object.values(obj))

let nuw_obj = obj.map(function(p){
 console.log(p.nombre)
})

I want to access all the values and not their keys

//Nombre...Coo..22
    
asked by afr 01.11.2017 в 23:45
source

5 answers

5

You can use Object.values (obj) by going through each item from your array of objects.

const personas = [
  { nombre: 'p1', apellidos: 'sona 1', edad: 1 },
  { nombre: 'p2', apellidos: 'sona 2', edad: 2 },
  { nombre: 'p3', apellidos: 'sona 3', edad: 2 }
];

personas.forEach(persona => console.log(Object.values(persona)));
    
answered by 02.11.2017 / 02:10
source
4

You could do it in these two ways:

1. Using only javaScript

const obj = [{
nombre: "Nombre...",
apellidos: "Coo..",
edad: 22
}];

let nuw_obj = obj.map(function(p){
    for (var item in p) {
        console.log(p[item])
    }
})

2. Using jQuery

const obj = [{
nombre: "Nombre...",
apellidos: "Coo..",
edad: 22
}];

let nuw_obj = obj.map(function(p){
    $.each(p, function(index, dato){
        console.log(dato)
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 01.11.2017 в 23:54
2

You can try the following:

var foo = [{
nombre: "Nombre...",
apellidos: "Coo..",
edad: 22
},
{
nombre: "Nombre2...",
apellidos: "Coo2..",
edad: 33
}];

for (var i = 0; i < foo.length; i++) {
	for (var key in foo[i]) {
		console.log( key + ": " + foo[0][key]);
	}
}

With this you move the object out of its values, plus you can also see that key are assigned (for future processes).

I hope it serves you.

    
answered by 02.11.2017 в 01:21
2

Why not try printing the object as such this way:

const obj = [{
  nombre: "Nombre...",
  apellidos: "Coo..",
  edad: 22
}];

console.log(Object.values(obj));

let nuw_obj = obj.map(function(p) {
  console.log(p);
});
    
answered by 01.11.2017 в 23:54
2

6 different ways:

const obj = {comunnity: 'Stack Overflow', from: 'Chile'};

/* Forma 1 */
for(var i in obj) console.log(obj[i])
/* Forma 2 */
for(var e of Object.values(obj)) console.log(e);
/* Forma 3 */
console.log(Object.values(new Array(obj)[new Array(obj).length-1]));
/* Forma 4 */
new Array(obj).map(e => {
 console.log(e);
});
/* Forma 5 */
new Array(obj).filter(e => {
  console.log(e);
});
/* Forma 6 */
Object.keys(obj).forEach(e => {console.log(obj[e])});
    
answered by 02.11.2017 в 10:33