0

I have have following selections:

<select id="custom-headers" multiple="multiple" class="searchable custom-headers">
<option data-invoice-number="36655" value="1187">RAPCY3</option>
<option data-invoice-number="87172" value="1188">1188</option>
<option data-invoice-number="79190" value="1189">CXETHD</option>
<option data-invoice-number="24584" value="1190">ZDAC2X</option>
<option data-invoice-number="49694" value="1191">2T8SRJ</option>
<option data-invoice-number="11290" value="1192">XDCH5J</option>
<option data-invoice-number="96188" value="1193">83EYS8</option>
<option data-invoice-number="33819" value="1194">WE5PEW</option>
<option data-invoice-number="56529" value="1195">CJEQWM</option>
<option data-invoice-number="55643" value="1196">RAPCY3</option>
<option data-invoice-number="72334" value="1197">1197</option>
<option data-invoice-number="14563" value="1198">CXETHD</option>
<option data-invoice-number="58963" value="1199">ZDAC2X</option>
<option data-invoice-number="44810" value="1200">2T8SRJ</option>
<option data-invoice-number="88482" value="1201">XDCH5J</option>
<option data-invoice-number="9731" value="1202">83EYS8</option>
<option data-invoice-number="41170" value="1203">WE5PEW</option>
<option data-invoice-number="1911" value="1204">CJEQWM</option>
</select>

I need to gather up both the values and invoice-number into one array.. jQuery works fine only for Reslist:

o.reservations        = [];
    $('.custom-headers option:selected').each(function (i, selected) {
        o.reservations[i] = $(selected).val();
});

Giving me back an array of 1187, 1188, 1189 i need something like

reslist => 1187, 1188, 1189, invoices => 36655, 87172, 79190

Tried:

o.reservations        = [];
    $('.custom-headers option:selected').each(function (i, selected) {
        o.reservations[i] = $(selected).val();
        o.reservations[i] = $(selected).data('invoice-number');
});

Not sure how to define the keys in js?

1
  • You should use map. It is the translation function. Commented Jan 4, 2016 at 16:07

3 Answers 3

1
$('.custom-headers option:selected').each(function (i, selected) {
    o.reservations.push({
        value: $(selected).val(),
        number: $(selected).data('invoice-number')
    });
});

This will populate the following array:

[{value: 1187, number: 36655}, {value: 1188, number: 87172}, ...]
Sign up to request clarification or add additional context in comments.

Comments

0
var arr = {"reslist":[], "invoices":[]};

$('.custom-headers option:selected').each(function (i, selected) {
    arr.reslist.push($(this).val());
    arr.invoices.push($(this).data("invoice-number"));
});

Comments

0

Use map.

console.log($('#custom-headers option').map(function(option){
    var $this = $(option);
    return { invoice-number: $this.data('invoice-number'), value: $this.val() };
}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.