如何替换对象数据中的空格?

时间:2015-12-08 10:53:52

标签: javascript jquery angularjs

我已经把时间安排在桌子上了。我想在时间和上午/下午之间添加空格如何添加空格。我存储的数据如下所示

var dealerSchedule = {
        "monday": "09:10AM - 06:30PM",
        "tuesday": "09:10AM - 12:00PM",
        "wednesday": "09:10AM - 06:30PM",
        "thursday": "09:10AM - 06:30PM",
        "friday": "09:10AM - 06:30PM",
        "saturday": "10:15AM - 04:15PM",
        "sunday": "10:15AM - 04:15PM"
      };

预期格式:

var dealerSchedule = {
        "monday": "09:10 AM - 06:30 PM",
        "tuesday": "09:10 AM - 12:00 PM",
        "wednesday": "09:10 AM - 06:30 PM",
        "thursday": "09:10 AM - 06:30 PM",
        "friday": "09:10 AM - 06:30 PM",
        "saturday": "10:15 AM - 04:15 PM",
        "sunday": "10:15 AM - 04:15 PM"
      };

3 个答案:

答案 0 :(得分:0)

是的,检查这个小提琴

enter link description here

var dealerSchedule = {
        "monday": "09:10AM - 06:30PM",
        "tuesday": "09:10AM - 12:00PM",
        "wednesday": "09:10AM - 06:30PM",
        "thursday": "09:10AM - 06:30PM",
        "friday": "09:10AM - 06:30PM",
        "saturday": "10:15AM - 04:15PM",
        "sunday": "10:15AM - 04:15PM"
      };

for ( var day in dealerSchedule )
{
  dealerSchedule[ day ] = dealerSchedule[ day ].split( "AM" ).join( " AM" );
  dealerSchedule[ day ] = dealerSchedule[ day ].split( "PM" ).join( " PM" );
}
console.log( dealerSchedule );

答案 1 :(得分:0)

试试这个



var dealerSchedule = {
  "monday": "09:10AM - 06:30PM",
  "tuesday": "09:10AM - 12:00PM",
  "wednesday": "09:10AM - 06:30PM",
  "thursday": "09:10AM - 06:30PM",
  "friday": "09:10AM - 06:30PM",
  "saturday": "10:15AM - 04:15PM",
  "sunday": "10:15AM - 04:15PM"
};

Object.keys(dealerSchedule).forEach(function (day) {
  dealerSchedule[day] = dealerSchedule[day].replace(/([A-Z]+)/g, ' $1')
});

console.log(dealerSchedule);




答案 2 :(得分:0)

我看到其他人的速度更快:)这是我的

http://codepen.io/mkdizajn/pen/Bjyrax?editors=001

// get keys
var k = Object.keys(dealerSchedule);
// loop and replace
for(var i = 0; i < k.length; i++){
    dealerSchedule[ k[ i ] ] = dealerSchedule[ k[i] ].replace('PM', ' PM').replace('AM', ' AM') ;
}