最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Push Values into an Object - Stack Overflow

programmeradmin0浏览0评论

What i'm trying to do is pushing values into an array like below:

var travellers = [['travellerUsername1', 'travellerFullname1'],['travellerUsername2', 'travellerFullname2'], ['travellerUsername3', 'travellerFullname3']]

$.each(travellers, function(index, value){
    options = [
        {label: value, title: value, value: index}
    ];
});

But as you can see this is just initiating the last data into my options object. How can i push each travellers value into my options object?

What i'm trying to do is pushing values into an array like below:

var travellers = [['travellerUsername1', 'travellerFullname1'],['travellerUsername2', 'travellerFullname2'], ['travellerUsername3', 'travellerFullname3']]

$.each(travellers, function(index, value){
    options = [
        {label: value, title: value, value: index}
    ];
});

But as you can see this is just initiating the last data into my options object. How can i push each travellers value into my options object?

Share Improve this question asked Aug 1, 2016 at 12:20 saimcansaimcan 1,7627 gold badges33 silver badges67 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Use Array#push method to push object in 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./ajax/libs/jquery/1.11.0/jquery.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论