我想要做的就是将值推送到如下数组:
var travellers = [['travellerUsername1', 'travellerFullname1'],['travellerUsername2', 'travellerFullname2'], ['travellerUsername3', 'travellerFullname3']]
$.each(travellers, function(index, value){
options = [
{label: value, title: value, value: index}
];
});
但正如您所看到的,这只是将最后一个数据启动到我的选项对象中。 如何将每位旅行者的价值推入我的期权对象?
答案 0 :(得分:3)
使用Array#push
方法推送object
array
var travellers = [
['travellerUsername1', 'travellerFullname1'],
['travellerUsername2', 'travellerFullname2'],
['travellerUsername3', 'travellerFullname3']
]
var options = [];
$.each(travellers, function(index, value) {
options.push({
label: value,
title: value,
value: index
});
});
console.log(JSON.stringify(options, null, 4));

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
&#13;