JSON数组必须包含时间angularjs应用格式的文本 [英] json array needs to be formatted based text containing time in angularjs application

查看:176
本文介绍了JSON数组必须包含时间angularjs应用格式的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度JS应用程序,我有以下的JSON数组

  $ scope.timeList = [
      {
        文:凌晨4:00
        值:17
      },
      {
        文:凌晨4:15
        值:18
      },
      {
        文:凌晨4:30
        值:19
      },
      {
        文:下午3:45
        值:64
      },
      {
        文:下午4:00
        值:65
      },
      {
        文:下午4:15
        值:66
      }
    ]

和我需要将其转换成以下格式:

  $ scope.timeFormattedList = [
      {
        文:凌晨4:00
        独一无二:4.00
        值:17
      },
      {
        文:凌晨4:15
        独一无二:4.15
        值:18
      },
      {
        文:凌晨4:30
        独一无二:4.30
        值:19
      },
      {
        文:下午3:45
        独一无二:15.45,
        值:64
      },
      {
        文:下午4:00
        独一无二:16.00,
        值:65
      },
      {
        文:下午4:15
        独一无二:16.15,
        值:66
      }
    ]

下面的新密钥独一无二被添加到基于文本键每个JSON对象。如果文本包含点,它应该增加12到它,否则相同的值仍然存在。


解决方案

您可以使用的 .MAP ,并获得独特的价值,您可以使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace\"相对=nofollow> .replace 法回调

\r
\r

变量$范围= {};\r
\r
$ scope.timeList = [{\r
  文:凌晨4:00\r
  值:17\r
},{\r
  文:凌晨4:15\r
  值:18\r
},{\r
  文:凌晨4:30\r
  值:19\r
},{\r
  文:下午3:45\r
  值:64\r
},{\r
  文:下午4:00\r
  值:65\r
},{\r
  文:下午4:15\r
  值:66\r
}];\r
\r
$ scope.timeFormattedList = $ scope.timeList.map(功能(EL){\r
  VAR独特= el.text.trim()代替(/(\\ d +):(\\ D +)\\ S(\\ w {2})/,\r
    功能(匹配,小时,分钟,meridiem){\r
      回报(meridiem ==='时'(+小时+ 12):小时)+'。 +分钟;\r
    });\r
\r
  返回{\r
    文本:el.text,\r
    独特之处:独特的,\r
    值:el.value\r
  }\r
});\r
\r
snippet.log(JSON.stringify($ scope.timeFormattedList));

\r

&LT;脚本SRC =htt​​p://tjcrowder.github.io/simple -snippets控制台/ snippet.js&GT;&LT; / SCRIPT&GT;

\r

\r
\r

In my angular js application, I have the following json array

 $scope.timeList =  [
      {
        "text": " 4:00 am",
        "value": "17"
      },
      {
        "text": " 4:15 am",
        "value": "18"
      },
      {
        "text": " 4:30 am",
        "value": "19"
      },
      {
        "text": " 3:45 pm",
        "value": "64"
      },
      {
        "text": " 4:00 pm",
        "value": "65"
      },
      {
        "text": " 4:15 pm",
        "value": "66"
      }
    ]

And I need to convert it into the following format :

 $scope.timeFormattedList =   [
      {
        "text": " 4:00 am",
        "unique" : "4.00",
        "value": "17"
      },
      {
        "text": " 4:15 am",
        "unique" : "4.15",
        "value": "18"
      },
      {
        "text": " 4:30 am",
        "unique" : "4.30",
        "value": "19"
      },
      {
        "text": " 3:45 pm",
        "unique" : "15.45",
        "value": "64"
      },
      {
        "text": " 4:00 pm",
        "unique" : "16.00",
        "value": "65"
      },
      {
        "text": " 4:15 pm",
        "unique" : "16.15",
        "value": "66"
      }
    ]

Here the new key "unique" is added to each json object based "text" key. If the "text" contains "pm" it should add 12 to it, otherwise same value remains.

解决方案

You can use .map, and to get unique value you can use .replace method with callback

var $scope = {};

$scope.timeList = [{
  "text": " 4:00 am",
  "value": "17"
}, {
  "text": " 4:15 am",
  "value": "18"
}, {
  "text": " 4:30 am",
  "value": "19"
}, {
  "text": " 3:45 pm",
  "value": "64"
}, {
  "text": " 4:00 pm",
  "value": "65"
}, {
  "text": " 4:15 pm",
  "value": "66"
}];

$scope.timeFormattedList = $scope.timeList.map(function (el) {
  var unique = el.text.trim().replace(/(\d+):(\d+)\s(\w{2})/, 
    function (match, hour, minutes, meridiem) {
      return (meridiem === 'pm' ? (+hour + 12) : hour) + '.' + minutes;
    });

  return {
    text: el.text,
    unique: unique,        
    value: el.value
  }
});

snippet.log(JSON.stringify($scope.timeFormattedList));

<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

这篇关于JSON数组必须包含时间angularjs应用格式的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆