需要在angularjs应用程序中基于包含时间的文本格式化json数组

时间:2015-11-28 12:58:27

标签: javascript angularjs json

在我的角度js应用程序中,我有以下json数组

 $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 =   [
      {
        "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"
      }
    ]

这里,新的键“unique”被添加到每个基于json对象的“text”键。如果“text”包含“pm”,则应该向其添加12,否则保留相同的值。

4 个答案:

答案 0 :(得分:1)

试试这个

$scope.timeFormattedList = $scope.timeList.map(function(item){
  var textArray = item.text.trim().split(" ");
  var timeArray = textArray[0].split(":");
  if(textArray[1] === "pm") {
    if(timeArray[0] != 12) {
      item.unique = parseInt(timeArray[0])+12+":"+timeArray[1];
    } else {
      item.unique = textArray[0]; 
    }
  } else {
    if(timeArray[0] != 12) {
      item.unique = textArray[0];
    } else {
      item.unique = "00:"+timeArray[1]; 
    }
  }
  return item;
});

在上午12点和下午12点时间添加了具体处理。

答案 1 :(得分:1)

最简单的方法是使用map()方法。

$scope.timeFormattedList = $scope.timeList.map(function(value){
  var match = value.text.match(/(\d+):(\d+)\s*(\w+)/);
  var nvalue = {
    'text': value.text,
    'unique': (match[3] == 'pm' ? parseInt(match[1]) + 12 : match[1]) + "." + match[2],
    'value': value.value
  };
  return nvalue
});

plunker code here

答案 2 :(得分:1)

您可以使用.map,为了获得唯一价值,您可以使用.replace方法进行回调



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView 
    android:id="@+id/name"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    />

<Button
    android:id="@+id/btn_delete"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_toRightOf="@id/name"
    android:layout_toEndOf="@id/name"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="Delete"></Button>
&#13;
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));
&#13;
&#13;
&#13;

答案 3 :(得分:1)

试试这个:

&#13;
&#13;
var 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"
      }
    ];
timeList.forEach(function(obj) {
  var m = obj.text.match(/ ([0-9]+):([0-9]+) (.+)/);
  console.log(m[3]);
  if (m[3] == 'pm') {
    m[1] = +m[1]+12;
  }
  obj.unique = m[1] + "." + m[2];
});
document.getElementById('output').innerHTML = JSON.stringify(timeList);
&#13;
<div id="output"></div>
&#13;
&#13;
&#13;