I have an array containing some objects, and I'm trying to skip it, where I have data stored in the following order:
firstName: Alice
lastName: Wonderland
age: 12
I try to execute a loop, and then sort it in descending order, when it age: valueshould be in the first position, then> lastName: WonderlandfirstName appears and finally.
Here is my code up to this point
var data = {
example1: [{
firstName: 'Alice',
lastName: 'Wonderland',
age: 12
}],
example2: [{
firstName: 'Thomas',
lastName: 'Mathison',
age: 14
}],
example3: [{
firstName: 'David',
lastName: 'Jacobsen',
age: 18
}]
};
for (var key in data) {
var arr = data[key];
for (var i = 0; i < arr.length; i++) {
var obj = arr[i];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
console.log(prop + ': ' + obj[prop]);
}
}
}
}
Run codeHide resultI want to get the reverse order (descending) when I post the result in console.log();:
age: 12,
lastName: 'Wonderland',
firstName: 'Alice'
age:14,
lastName: 'Mathison',
firstName: 'Thomas'
age:18,
lastName: 'Jacobsen',
firstName: 'David'
I am not sure about the behavior of the sort function. How should it work during a cycle?
Any suggestions?