使用`.map()`从数组中创建一组选项。

huangapple go评论59阅读模式
英文:

Items from array using .map() into a group of options

问题

我有一个使用jQuery的.map()方法创建的数组:

var options = $('#' + id + ' option'); //动态生成的选择框的ID
var values = $.map(options, function(option) {
    return [
        option.value,
        option.text
    ];
});

这会创建一个数组,例如:"03","rents", "04", "other income"等。当我尝试将它们放回选择标签中,经过一些其他操作后,使用数组作为选项:

//清空选项并重新构建
$('#' + id).empty();
var list = $('#' + id);
$.each(values, function(key, value) {
    list.append($('<option>', { value: key })
        .text(value));
});

我得到每个值都是文本,每个文本也都是文本:

<option value="0">3</option>
<option value="1">rents</option>
<option value="2">4</option>
<option value="3">other income</option>

我该如何让数字作为值,文本作为文本,而不是它们都作为文本?

<option value="3">rents</option>
<option value="4">other income</option>
英文:

I have an array that is created using jquery .map():

var options = $(&#39;#&#39; + id + &#39; option&#39;); //Id of select that is generated dynamically
        values = $.map(options, function(option) {
            return [
                option.value,
                option.text
            ];
        });

This creates an array of &quot;03&quot;,&quot;rents&quot;, &quot;04&quot;, &quot;other income&quot; etc. When I attempt to put these back into a select tag, after some other work, with the array as the options using

//empty the options and rebuild
            $(&#39;#&#39; + id).empty();
            var list = $(&#39;#&#39; + id);
            $.each(values, function(key, value) {
                list.append($(&#39;&lt;option&gt;&#39;, { value: key })
                    .text(value));
            });

I am getting each value as a text and each text as text:

&lt;option value=&quot;0&quot;&gt;3&lt;/option&gt;
&lt;option value=&quot;1&quot;&gt;rents&lt;/option&gt;
&lt;option value=&quot;2&quot;&gt;4&lt;/option&gt;
&lt;option value=&quot;3&quot;&gt;other income&lt;/option&gt;

How can I get it so that the numbers are my values and the text is the text, and not have them all as the text?

&lt;option value=&quot;3&quot;&gt;rents&lt;/option&gt;
&lt;option value=&quot;4&quot;&gt;other income&lt;/option&gt;

答案1

得分: 2

$.map()会自动将数组展平,所以有一点小技巧;当你使用each重新创建时,key代表索引,而value实际上是你的数据。

id = 1;
var options = $('#' + id + ' option'); //生成动态生成的选择器的ID
values = $.map(options, function(option) {
  return [[
    option.value,
    option.text
  ]]; //将其放入另一个数组中,展平后你将获得数组列表
});
console.log(values);

// 你会得到一个类似于[[1,2],["ads","erere"]]的数组列表
// 这就是为什么要用value[0]或value[1]
$('#' + id).empty();
var list = $('#' + id);
$.each(values, function(key, value) {
  list.append($('<option>', { value: value[0] })
              .text(value[1])); 
});
英文:

So there are 2 things.

$.map() automatically make array flatten... so there is kind of a trick to it ;]

and when you recreate using each, key stands for index, and actually value is your data.

id = 1;
var options = $(&#39;#&#39; + id + &#39; option&#39;); //Id of select that is generated dynamically
values = $.map(options, function(option) {
  return [[
    option.value,
    option.text
  ]]; // put it into another array, when flatten you will get list of arrays
});
console.log(values);
        
// you have list of array like [[1,2],[&quot;ads&quot;,&quot;erere&quot;]]
// thats why value[0] or value[1]        
$(&#39;#&#39; + id).empty();
var list = $(&#39;#&#39; + id);
$.each(values, function(key, value) {
  list.append($(&#39;&lt;option&gt;&#39;, { value: value[0] })
              .text(value[1])); 
});

huangapple
  • 本文由 发表于 2020年1月6日 22:29:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定