Reputation: 4842
I am new in json and lodash. I want to get two values from Json. But right now i am getting only one value from return. So I want to get together both values.
[
{
"_id": {
"mm": 2,
"hr": 9,
"sl": 1
},
"tot": 188
},
{
"_id": {
"mm": 2,
"hr": 4,
"sl": 3
},
"tot": 622
},
{
"_id": {
"mm": 2,
"hr": 12,
"sl": 5
},
"tot": 484
}]
_.map(resp.data,function(res){ return res._id.hr})
I want to also get sl value with "hr" Like 9:1, 4:3, 12:5. (hr:sl).
Upvotes: 0
Views: 436
Reputation: 50
Hope this help:
var json = [
{
"_id":{
"mm":2,
"hr":9,
"sl":1
},
"tot":188
},
{
"_id":{
"mm":2,
"hr":4,
"sl":3
},
"tot":622
},
{
"_id":{
"mm":2,
"hr":12,
"sl":5
},
"tot":484
}
]
$data = _.map(json,function(res){ return (res._id.hr + ':' + res._id.sl)})
console.log($data);
https://jsfiddle.net/dedenbangkit/qcqbsb69/
Ok, if you want to put another ten minutes for the _.id.sl variables, you can do this:
$data = _.map(json,function(res){
$hour = res._id.hr;
$minute = (res._id.sl*10) + 10;
if ($minute >= 60)
{
$minute = '00';
$hour = $hour +1;
}
return ($hour + ':' + $minute)
})
Here's the fiddle: https://jsfiddle.net/dedenbangkit/qcqbsb69/1/
Sorry, I think you don't want 24 become 25 for hour format isn't it? so this will might better for you, assuming that 00:00 is middle night.
$data = _.map(json,function(res){
$hour = res._id.hr;
$minute = (res._id.sl*10) + 10;
if ($minute > 50){
$minute = '0' + (60 - $minute);
$hour = $hour + 1;
}else{
$hour = $hour + 1;
}
if($hour > 24){
$hour = '0' + (- 24 + $hour);
}
return ($hour + ':' + $minute)
})
Another update: https://jsfiddle.net/dedenbangkit/qcqbsb69/4/
Upvotes: 1
Reputation: 2308
You can return an object with all the data you want, for example:
var results =_.map(a, function(res) {
return { hr: res._id.hr, sl: res._id.sl };
});
Upvotes: 2
Reputation: 2278
Using Vanilla JS
var arr = [
{
"_id": {
"mm": 2,
"hr": 9,
"sl": 1
},
"tot": 188
},
{
"_id": {
"mm": 2,
"hr": 4,
"sl": 3
},
"tot": 622
},
{
"_id": {
"mm": 2,
"hr": 12,
"sl": 5
},
"tot": 484
}]
var res = arr.map((item) => item._id.hr + " : " + item._id.sl )
console.log(res)
Using loaddash
var arr = [{
"_id": {
"mm": 2,
"hr": 9,
"sl": 1
},
"tot": 188
},
{
"_id": {
"mm": 2,
"hr": 4,
"sl": 3
},
"tot": 622
},
{
"_id": {
"mm": 2,
"hr": 12,
"sl": 5
},
"tot": 484
}]
var res = _.map(arr, (item) => item._id.hr + " : " + item._id.sl )
console.log(res);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
Upvotes: 0