Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Push Values into an 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?

like image 519
saimcan Avatar asked Apr 09 '26 00:04

saimcan


1 Answers

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.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
like image 103
Rayon Avatar answered Apr 11 '26 13:04

Rayon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!