通过使用JS,我试图得到一个二维数组,我有一个数组,如下所示:
array [ "2016/03/31", "2016/03/30", "2016/03/29", "2016/03/28", "2016/03/27", "2016/04/01"]
寻找输出
array [
{'date':'2016/03/22'},
{'date':'2016/03/23'},
{'date':'2016/03/24'},
{'date':'2016/03/25'},
{'date':'2016/03/26'},
{'date':'2016/03/27'},
{'date':'2016/03/28'},
{'date':'2016/03/29'}
];
JS
function getarryDates (list)
{
var aryDates = [];
var Dates_ary = [];
$.each(list, function(i, e) {
Dates_ary[0] = aryDates.push("'date:'"+ e);
});
return Dates_ary;
}
答案 0 :(得分:2)
答案 1 :(得分:0)
如果您想以最小的更改来完成,您可以这样做:
function getarryDates (list)
{
var aryDates = [];
var Dates_ary = [];
$.each(list, function(i, e) {
Dates_ary[i] = {"date": e }; // <-- use index 'i' and
// create object instead of str.
});
return Dates_ary;
}
但是,使用map
的解决方案更好:它更干净,更易于理解且更易于维护。