I have an object array return from mongoose find method. However it is in below format:
var value = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
How can I convert it to below:
var value = [{'year': 2018, 'month':3, 'count': 123}];
I tried below to convert but still print out the original format.
var result = [];
result.push(value);
var arr = [];
Object.keys(result).forEach(function(item) {
arr.push(result[item]);
console.log(result[item].year); //undefined
console.log(result[item]['year']; //undefined
})
console.log(arr);
Below is the retrieving method.
function findStats(result, cb) {
monthly_order.find({})
.select('-_id')
.sort({
"_id.year": 1,
"_id.month": 1
})
.exec(function (err, result) {
if (err) {
console.log('Error find: ', err);
return cb(err);
}
if(!result){
return cb({'message':'No Order.'});
}else{
console.log(result);
return cb(null, result);
//var arr = result.map(x => x.value);
//console.log(arr);
//return cb(null, arr);
}
});
}
Using .map() solution enter image description here
Original output enter image description here
4 Answers 4
Just .map the .value properties of every item in the original array to the new array:
var valueArr = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
var newArr = valueArr.map(({ value }) => value);
console.log(newArr);
Note that you'll still only have a single object in memory here - mutations to one variable name will result in mutations to the other variable name. If you want to avoid that, spread the value object in the .map callback instead:
var valueArr = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
var newArr = valueArr.map(({ value }) => ({ ...value }));
console.log(newArr);
Comments
You're looking to modify each element of an array in the same fashion. This is a use-case for .map().
var value = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
var result = value.map(x => x.value); //For each item (x), get the "value" property
console.log(result);
The code in your question is returning undefined because result[item] refers to this:
{'value': {'year': 2018, 'month':3, 'count': 123}}
As you can see, in order to access the year property, you'd have to first access the value property:
result[item]["value"]["year"]
10 Comments
undefined.result is not what you think it is. On the line before var arr = result.map(x => x.value); , do console.log(result), and let us know what it shows. You're taking result as an argument in findStats, but then redefining it as the result of .exec...that seems strange. You're never using the result that you pass findStats.Iterate over the array any push the value of the object to a new array
var value = [{
'value': {
'year': 2018,
'month': 3,
'count': 123
}
}];
let newVal = [];
value.forEach(function(item) {
for (let keys in item) {
newVal.push(item[keys])
}
})
console.log(newVal)
How can I convert it to below: var value = [{'year': 2018, 'month':3, 'count': 123}];
Comments
var value = [{'value': {'year': 2018, 'month':3, 'count': 123}}];
var result = [];
value.map(function(val,i){
for(var j in val){
result.push(val[j])
}
})
resultthat you pass tofindStatsgoes completely unused, as it's redefined by.exec. Are you sure thatresult, inside the.exec(function(err, result) { ... }), is what you think it is?