JS将具有日期名称的数组转换为日期编号

时间:2017-07-13 16:21:14

标签: javascript arrays

将日名转换为日期位置的最佳方法是什么。

我有阵列:

var days = ['Monday','Sunday','Friday']

如何转换为:newArray = [0,6,4]

我试试这个:

function getClosedDates() {
         var url = "/getClosedDates"; // the script where you handle the form input.
         var WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var newArray = [];
var days = [];
  $.ajax({
           type: "GET",
           url: url,
           dataType: "json",
           success: function(data)
           {

 days = data.data.ClosedDays;
newArray = days.map(function(day) {
  return WEEKDAYS.indexOf(day);
});
           }
         });
return newArray;
};

但是我得到了[],空数组......为什么?

2 个答案:

答案 0 :(得分:3)

var WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

var days = ['Monday','Sunday','Friday'];

var newArray = days.map(function(day) {
  return WEEKDAYS.indexOf(day);
})

答案 1 :(得分:0)

试试这个:

<div class="circle-singleline">
  2017
</div>
<div class="text">
  sdfs df sdfg sdf gsd fg sdf g sdfg  sdfg sdfg 
</div>

<div class="circle-singleline">
  2016
</div>
<div class="text">
  sdfs df sdfg sdf gsd fg sdf g sdfg  sdfg sdfg 
</div>
<div class="circle-singleline">
  2015
</div>
<div class="text">
  sdfs df 

sdfg sdf gsd fg sdf g sdfg  sdfg sdfg 
</div>