数组中的拼接数据

时间:2018-10-15 06:11:53

标签: javascript jquery

我正在从事有关数据映射的项目。实现了几项检查:

  • 导入好的文件

  • 餐桌选择

  • 列表的选择

  • 数据分页

我目前正在选择列。我将这些各种各样的选择存储在阵列中。问题是,如果我要删除阵列中的一个选项,则会删除所有数据! 我正在使用此插件:http://wenzhixin.net.cn/p/multiple-select/docs/

var choiceFields = [];                  
$('#selectFields').multipleSelect({
    filter: true,
    onClick: function(view) 
    {
        choiceFields.push(view.value);
        var length = choiceFields.length-1;
        if(view.checked === false)
        {
            choiceFields.splice(view.value);
        }
        console.log(choiceFields);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.css" rel="stylesheet"/>
<script src="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.js"></script>
<div class="select-box">
    <label for="selectFields"><span class="label-FieldChoice">Choice fields</span>  </label>
    <select id="selectFields" multiple="multiple" style="display: none;">
        <option value="id">id</option>
        <option value="username">username</option>
        <option value="username_canonical">username_canonical</option>
        <option value="email">email</option>
        <option value="email_canonical">email_canonical</option>
        <option value="enabled">enabled</option>
        <option value="salt">salt</option>
        <option value="password">password</option>
        <option value="last_login">last_login</option>
        <option value="confirmation_token">confirmation_token</option>
        <option value="password_requested_at">password_requested_at</option>
        <option value="roles">roles</option>
        <option value="lastName">lastName</option>
        <option value="firstName">firstName</option>
    </select>
    
</div>

2 个答案:

答案 0 :(得分:6)

您可以这样做:

var choiceFields = [];
$('#selectFields').multipleSelect({
  filter: true,
  onClick: function(view) {
    choiceFields.push(view.value);
    var length = choiceFields.length - 1;
    if (view.checked === false) {
      choiceFields = jQuery.grep(choiceFields, function(value) {
        return value != view.value;
      });
    }
    console.log(choiceFields);
  }
});

我已更改:

choiceFields.splice(view.value);

收件人:

choiceFields = jQuery.grep(choiceFields, function(value) {
  return value != view.value;
});

演示

var choiceFields = [];
$('#selectFields').multipleSelect({
  filter: true,
  onClick: function(view) {
    choiceFields.push(view.value);
    var length = choiceFields.length - 1;
    if (view.checked === false) {
      choiceFields = jQuery.grep(choiceFields, function(value) {
        return value != view.value;
      });
    }
    console.log(choiceFields);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.css" rel="stylesheet" />
<script src="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.js"></script>
<div class="select-box">
  <label for="selectFields"><span class="label-FieldChoice">Choice fields</span>  </label>
  <select id="selectFields" multiple="multiple" style="display: none;">
    <option value="id">id</option>
    <option value="username">username</option>
    <option value="username_canonical">username_canonical</option>
    <option value="email">email</option>
    <option value="email_canonical">email_canonical</option>
    <option value="enabled">enabled</option>
    <option value="salt">salt</option>
    <option value="password">password</option>
    <option value="last_login">last_login</option>
    <option value="confirmation_token">confirmation_token</option>
    <option value="password_requested_at">password_requested_at</option>
    <option value="roles">roles</option>
    <option value="lastName">lastName</option>
    <option value="firstName">firstName</option>
  </select>

</div>

答案 1 :(得分:1)

Array.splice()中,它采用3个参数array.splice(index, howMany, [element1][, ..., elementN])

索引-开始更改数组的索引。

howMany -一个整数,指示要删除的旧数组元素的数量。如果howMany为0,则不会删除任何元素。

element1,...,elementN -要添加到数组中的元素。如果您未指定任何元素,则拼接只会从数组中删除这些元素。

但是您没有在答案中提供howMany参数,请提供此值以从数组中删除特定索引

ex-

var choiceFields = [];                  
$('#selectFields').multipleSelect({
filter: true,
onClick: function(view) 
{
   
    var length = choiceFields.length-1;
    if(view.checked === false)
    {
        var index = choiceFields.indexOf(view.value);  // Finding Index of Item

        choiceFields.splice(index,1);   // Splicing one element from index
    }
    else{
     choiceFields.push(view.value);  // if false we do not push value
    }
    console.log(choiceFields);
},

onCheckAll: function() 
{
     
     
     choiceFields = [];    // delete all previous values
     var options = document.getElementById('selectFields');
     for(var i=0;i<options.length;i++){
        choiceFields.push(options[i].text);
     }
   
   console.log(choiceFields);

}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.css" rel="stylesheet"/>
<script src="https://rawgit.com/wenzhixin/multiple-select/master/multiple-select.js"></script>
<div class="select-box">
    <label for="selectFields"><span class="label-FieldChoice">Choice fields</span>  </label>
    <select id="selectFields" multiple="multiple" style="display: none;">
        <option value="id">id</option>
        <option value="username">username</option>
        <option value="username_canonical">username_canonical</option>
        <option value="email">email</option>
        <option value="email_canonical">email_canonical</option>
        <option value="enabled">enabled</option>
        <option value="salt">salt</option>
        <option value="password">password</option>
        <option value="last_login">last_login</option>
        <option value="confirmation_token">confirmation_token</option>
        <option value="password_requested_at">password_requested_at</option>
        <option value="roles">roles</option>
        <option value="lastName">lastName</option>
        <option value="firstName">firstName</option>
    </select>
    
</div>