0
最近我問了一個類似的問題,但我會刪除它,因爲它不夠清楚,我的錯誤。從Object中獲取值,將它們放在一個由逗號分隔的數組中
我需要從多個動態對象
Object {year: 2017, month: 1, date: 26}
Object {year: 2017, month: 2, date: 5}
Object {year: 2017, month: 2, date: 12}
Object {year: 2017, month: 2, date: 19}
Object {year: 2017, month: 2, date: 26}
獲得年份,月份和日期值,我需要讓他們在陣列像這樣:
[26/1/2017, 5/2/2017, 12/2/2017, 19/2/2017, 26/2/2017]
比,使用數組把這些值置於輸入隱藏值中,如下所示:
<input type="hidden" value="26/1/2017, 5/2/2017, 12/2/2017, 19/2/2017, 26/2/2017">
我設法獲取值並將它們推入陣列,但我不能讓他們的工作,因爲它應該,這裏是我的代碼:
// Note: This code is in each function
// This holds the objects
var sundayData = $(this).data();
// I get date, month and year values
var dayAll = sundayData['date'];
var monthAll = sundayData['month'];
var yearAll = sundayData['year'];
// Here, I putting them in the right format
var sundayDatesAll = dayAll + '/' + (monthAll + 1) + '/' + yearAll;
//
var sundayDate = [];
// Here, I push the dates in sundayDate
sundayDate.push(sundayDatesAll);
$('input').val(sundayDate.join());
但是,像這樣,它返回分離數組是這樣的:
["26/2/2017"]
["5/3/2017"]
["12/3/2017"]
["19/3/2017"]
["26/3/2017"]
,只有有一個在輸入值中,我認爲它的最後一個值26/3/2017
任何幫助表示讚賞,感謝...
使用concat()方法來合併陣列 –