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 01 Answer
Reset to default 5Use 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>