Printing object's keys and values
for (key in filters[0]){
console.log( key + ": " + filters[0][key]);
}
Or if you want to print all the values of filters
for (i in filters){
console.log(i);
for (key in filters[i]){
console.log( key + ": " + filters[i][key]);
}
}
On @mplungjan 's comment
filters.forEach(function(obj, index){
console.log(index);
for (var key in obj){
console.log(key, obj[key]);
}
});
This is looking for a term
property on filters[0]
:
console.log(filters[0].term);
What you actually want to do is use the value of term
(in your example that will be "user"
) as the property identifier:
console.log(filters[0][term]);
for loop for array and for..in iteration for object:
var filters = [{ "user": "abc"}, {"application": "xyz"}];
for (var i = 0; i < filters.length; i++) { // the plainest of array loops
var obj = filters[i];
// for..in object iteration will set the key for each pair
// and the value is in obj[key]
for (var key in obj) {
console.log(key, obj[key])
}
}
ES6
[{ "user": "abc"}, {"application": "xyz"}].forEach(
obj => console.log(Object.entries(obj).flat())
)