Reputation: 603
var obj = [
{
"amount": " 12185",
"job": "GAPA",
"month": "JANUARY",
"year": "2010"
},
{
"amount": "147421",
"job": "GAPA",
"month": "MAY",
"year": "2010"
},
{
"amount": "2347",
"job": "GAPA",
"month": "AUGUST",
"year": "2010"
}
]
How can i get all amounts, that is '12185'
, '147421'
, '2347'
.
I've tried to do this
Object.keys(obj).map(key => obj[key])
Upvotes: 0
Views: 54
Reputation: 31
Your obj
variable is actually an array. (denoted by the braces []
)
You can use the map function on an array to return what you're asking for.
var amounts = obj.map(x => x.amount); // Array(3) [ " 12185", "147421", "2347" ]
You may also want to append .trim() to the end of x.amount to remove any spaces specifically.
var amounts = obj.map(x => x.amount.trim()); // Array(3) [ "12185", "147421", "2347" ]
var obj = [
{
"amount": " 12185",
"job": "GAPA",
"month": "JANUARY",
"year": "2010"
},
{
"amount": "147421",
"job": "GAPA",
"month": "MAY",
"year": "2010"
},
{
"amount": "2347",
"job": "GAPA",
"month": "AUGUST",
"year": "2010"
}
];
var amounts = obj.map(x => x.amount);
console.log(keys); // Array(3) [ " 12185", "147421", "2347" ]
Upvotes: 2
Reputation: 368
I believe you're looking for this
var obj = [
{
"amount": " 12185",
"job": "GAPA",
"month": "JANUARY",
"year": "2010"
},
{
"amount": "147421",
"job": "GAPA",
"month": "MAY",
"year": "2010"
},
{
"amount": "2347",
"job": "GAPA",
"month": "AUGUST",
"year": "2010"
}
];
let amounts = obj.map(v => v.amount);
console.log(amounts);
Upvotes: 1
Reputation: 8455
Your obj
is an array, you'll need:
const vals = obj.map(entry => entry.amount); // ["12185", "147421", "2347"]
Upvotes: 1