Wynn Teo
Wynn Teo

Reputation: 190

How to access object array values in Javascript

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

Upvotes: 1

Views: 1867

Answers (4)

Tyler Roper
Tyler Roper

Reputation: 21672

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"]

Upvotes: 1

李万金
李万金

Reputation: 11

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

Upvotes: 0

brk
brk

Reputation: 50346

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

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 371168

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

Upvotes: 3

Related Questions