1

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);
 }
 }); 
 }

enter image description here

Using .map() solution enter image description here

Original output enter image description here

asked Dec 12, 2018 at 2:06
1
  • Now that you've included your full block of code, I'm noticing an issue. The result that you pass to findStats goes completely unused, as it's redefined by .exec. Are you sure that result, inside the .exec(function(err, result) { ... }), is what you think it is? Commented Dec 12, 2018 at 2:28

4 Answers 4

3

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);

answered Dec 12, 2018 at 2:08

Comments

1

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"]
answered Dec 12, 2018 at 2:09

10 Comments

Not sure why the console print out undefined.
Can you share the code you're trying to use? As you can see in the snippet above, it should not output undefined.
Okay, just included a snapshot of the original output in my question.
Updated the retrieve method in my question, you can see the console.log(arr) actually return me undefined.
Then 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.
|
0

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}];

answered Dec 12, 2018 at 2:13

Comments

0

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])
 }
})

answered Dec 12, 2018 at 2:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.