Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过lodash从json对象获取两个值?_Javascript_Json_Lodash - Fatal编程技术网

Javascript 如何通过lodash从json对象获取两个值?

Javascript 如何通过lodash从json对象获取两个值?,javascript,json,lodash,Javascript,Json,Lodash,我是json和lodash方面的新手。我想从Json中得到两个值。但现在我只从return中得到一个值。所以我想把这两种价值观结合起来 [ { "_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

我是json和lodash方面的新手。我想从Json中得到两个值。但现在我只从return中得到一个值。所以我想把这两种价值观结合起来

[
{
"_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


 }]


我还想用“hr”得到sl值,比如9:1,4:3,12:5。(hr:sl)。

您可以返回包含所有所需数据的对象,例如:

var results =_.map(a, function(res) { 
  return { hr: res._id.hr, sl: res._id.sl }; 
});

您可以返回包含所有所需数据的对象,例如:

var results =_.map(a, function(res) { 
  return { hr: res._id.hr, sl: res._id.sl }; 
});
希望这有助于:

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

好的,如果您想为uu.id.sl变量再花十分钟,您可以这样做:

$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)
})
这是小提琴:

对不起,我想你不希望24小时制变成25小时制,不是吗? 所以这对你来说可能更好,假设00:00是午夜

$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)
})
另一个更新:

希望此帮助:

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

好的,如果您想为uu.id.sl变量再花十分钟,您可以这样做:

$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)
})
这是小提琴:

对不起,我想你不希望24小时制变成25小时制,不是吗? 所以这对你来说可能更好,假设00:00是午夜

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

另一个更新:

使用香草JS

var-arr=[
{
“_id”:{
“mm”:2,
“hr”:9,
“sl”:1
},
“tot”:188
},
{
“_id”:{
“mm”:2,
“人力资源”:4,
“sl”:3
},
“tot”:622
},
{
“_id”:{
“mm”:2,
“hr”:12,
“sl”:5
},
“tot”:484
}]
var res=arr.map((item)=>item.\u id.hr+“:”+item.\u id.sl)

console.log(res)
使用香草JS

var-arr=[
{
“_id”:{
“mm”:2,
“hr”:9,
“sl”:1
},
“tot”:188
},
{
“_id”:{
“mm”:2,
“人力资源”:4,
“sl”:3
},
“tot”:622
},
{
“_id”:{
“mm”:2,
“hr”:12,
“sl”:5
},
“tot”:484
}]
var res=arr.map((item)=>item.\u id.hr+“:”+item.\u id.sl)

console.log(res)
我们可以在“sl”中加1吗?然后它变成9.10到9:20,4:30到4:40,12:50到13:00实际上这是hr:min,我想显示9:10--9:20。再加10分钟。“sl”是指像这样的微小的事情吗$data=u.map(json,函数(res){$hour=res.\u id.hr;$minute=(res.\u id.sl*10)+10;如果($minute>=60){$minute='00';$hour=$hour+1;}return($hour+':'+$minute})我们能在“sl”中加上1吗?那么它就变成了9.10到9:20,4:30到4:40,12:50到13:00实际上这是hr:min,我想显示9:10--9:20。再加10分钟。“sl”是指像这样的微小的事情吗$data=u.map(json,函数(res){$hour=res.\u id.hr;$minute=(res.\u id.sl*10)+10;if($minute>=60){$minute='00';$hour=$hour+1;}return($hour+':'+$minute})我想用res\u id.sl添加1。你的意思是在sl的值上加1吗?如果是这样的话,你会返回一个对象,比如:{hr:res.\u id.hr,sl:res.\u id.sl+1}我已经做了这些事情,但是显示为12:31,但我想要12:40。你能在JSBin中显示吗?我已经更新了JSBin,以显示以组合字符串的形式返回值的示例,如你的问题中所示,并增加其中一个值。你需要注意括号,因为js将连接“1”而不是添加它,因为它将结果视为字符串,而不是数字。谢谢你的回答。当12.6或13.6或14.6出现时,它可能会像13或14或15一样圆吗?我想用res.\u id.sl添加1。你的意思是在sl的值上加1吗?如果是这样的话,你会返回一个对象,比如:{hr:res.\u id.hr,sl:res.\u id.sl+1}我已经做了这些事情,但是显示为12:31,但我想要12:40。你能在JSBin中显示吗?我已经更新了JSBin,以显示以组合字符串的形式返回值的示例,如你的问题中所示,并增加其中一个值。你需要注意括号,因为js将连接“1”而不是添加它,因为它将结果视为字符串,而不是数字。谢谢你的回答。当12.6或13.6或14.6出现时,它会像13或14或15一样旋转吗