英文:
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 = $('#' + id + ' option'); //Id of select that is generated dynamically
        values = $.map(options, function(option) {
            return [
                option.value,
                option.text
            ];
        });
This creates an array of "03","rents", "04", "other income" 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
            $('#' + id).empty();
            var list = $('#' + id);
            $.each(values, function(key, value) {
                list.append($('<option>', { value: key })
                    .text(value));
            });
I am getting each value as a text and each text as text:
<option value="0">3</option>
<option value="1">rents</option>
<option value="2">4</option>
<option value="3">other income</option>
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?
<option value="3">rents</option>
<option value="4">other income</option>
答案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 = $('#' + id + ' option'); //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],["ads","erere"]]
// thats why value[0] or value[1]        
$('#' + id).empty();
var list = $('#' + id);
$.each(values, function(key, value) {
  list.append($('<option>', { value: value[0] })
              .text(value[1])); 
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论