Scenario: I have a variable JSON
var json = {
"order": {
"orderDetails": [{
"a1": "b1",
"c1": "d1",
"e1": "f1"
}, {
"a1": "b2",
"c1": "d2",
"e1": "f2"
}, {
"a1": "b3",
"c1": "d3",
"e1": "f3"
}],
"orderHeader": [{
"a2": "b1",
"c2": "d1",
"e2": "f1"
}, {
"a2": "b2",
"c2": "d2",
"e2": "f2"
}]
}
};
I need to get an array of all the values of order.orderdetails.a1
like
['b1', 'b2', 'b3']
Scenario: I have a variable JSON
var json = {
"order": {
"orderDetails": [{
"a1": "b1",
"c1": "d1",
"e1": "f1"
}, {
"a1": "b2",
"c1": "d2",
"e1": "f2"
}, {
"a1": "b3",
"c1": "d3",
"e1": "f3"
}],
"orderHeader": [{
"a2": "b1",
"c2": "d1",
"e2": "f1"
}, {
"a2": "b2",
"c2": "d2",
"e2": "f2"
}]
}
};
I need to get an array of all the values of order.orderdetails.a1
like
['b1', 'b2', 'b3']
Share
Improve this question
edited Jan 24, 2016 at 11:31
Tushar
87.2k21 gold badges163 silver badges181 bronze badges
asked Jan 24, 2016 at 10:03
Jibin TJJibin TJ
4461 gold badge4 silver badges15 bronze badges
2
-
all the vaues of order.orderdetails.a1
...order.orderdetails.a1
would be undefined – Jaromanda X Commented Jan 24, 2016 at 10:06 - @jaromandaX all the values of a1 means ,the values of a1 in array orderdetails – Jibin TJ Commented Jan 24, 2016 at 10:12
3 Answers
Reset to default 6As you've underscore.js, lodash included, why not taking advantage of them instead of reinventing the wheel.
How about single-line using _.map
. _.map
also accepts strings as iteratee and will return the value of that key from the passed object.
_.map(json.order.orderDetails, 'a1')
var json = {
"order": {
"orderDetails": [{
"a1": "b1",
"c1": "d1",
"e1": "f1"
}, {
"a1": "b2",
"c1": "d2",
"e1": "f2"
}, {
"a1": "b3",
"c1": "d3",
"e1": "f3"
}],
"orderHeader": [{
"a2": "b1",
"c2": "d1",
"e2": "f1"
}, {
"a2": "b2",
"c2": "d2",
"e2": "f2"
}]
}
};
var result = _.map(json.order.orderDetails, 'a1');
console.log(result);
document.getElementById('result').innerHTML = JSON.stringify(result, 0, 4); // For Demo: Showing the result on the screen
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.0.0/lodash.js"></script>
<pre id="result"></pre>
This is similar to _.pluck
in older versions of lodash.
_.pluck(json.order.orderDetails, 'a1')
The same result can be achieved in pure JavaScript using Array#map
json.order.orderDetails.map(e => e.a1)
var json = {
"order": {
"orderDetails": [{
"a1": "b1",
"c1": "d1",
"e1": "f1"
}, {
"a1": "b2",
"c1": "d2",
"e1": "f2"
}, {
"a1": "b3",
"c1": "d3",
"e1": "f3"
}],
"orderHeader": [{
"a2": "b1",
"c2": "d1",
"e2": "f1"
}, {
"a2": "b2",
"c2": "d2",
"e2": "f2"
}]
}
};
var result = json.order.orderDetails.map(e => e.a1);
console.log(result);
document.getElementById('result').innerHTML = JSON.stringify(result, 0, 4);
<pre id="result"></pre>
You can use this code to do so.
var json={
"order": {
"orderDetails": [
{
"a1": "b1",
"c1": "d1",
"e1": "f1"
},
{
"a1": "b2",
"c1": "d2",
"e1": "f2"
},
{
"a1": "b3",
"c1": "d3",
"e1": "f3"
}
],
"orderHeader": [
{
"a2": "b1",
"c2": "d1",
"e2": "f1"
},
{
"a2": "b2",
"c2": "d2",
"e2": "f2"
}
]
}
}
var a1 = json.order.orderDetails.map(function(obj){ return obj.a1 });
console.log(a1);
You can try something like this:
var json = {
"order": {
"orderDetails": [{
"a1": "b1",
"c1": "d1",
"e1": "f1"
}, {
"a1": "b2",
"c1": "d2",
"e1": "f2"
}, {
"a1": "b3",
"c1": "d3",
"e1": "f3"
}],
"orderHeader": [{
"a2": "b1",
"c2": "d1",
"e2": "f1"
}, {
"a2": "b2",
"c2": "d2",
"e2": "f2"
}]
}
}
var result = {};
// Loop over props of "order"
for (var order in json.order){
// Each prop is array. Loop over them
json.order[order].forEach(function(item) {
// Loop over each object's prop
for (var key in item) {
// Check if result has a prop with key. If not initialize it.
if (!result[key])
result[key] = [];
// Push vaues to necessary array
result[key].push(item[key]);
}
})
};
console.log(result);