Splice data in array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I'm working on a project on data mapping. Several checks are realized:



  • well imported file


  • table choice


  • columns choice of the table


  • typage of data


I'm for at the part of the choice of columns for the moment. I'm stocking these various choices in an array. The problem is that if I want to delete one choice in my array, all data are deleted !
I'm using this plugin: http://wenzhixin.net.cn/p/multiple-select/docs/



Edit: I'm trying now to make an event when all the options are checked by either clicking the “Select all” checkbox, or when the “checkall” method is programatically called. The problem is that I don't see how to get all these checkbox content






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;
);

var contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
,
onCheckAll: function()

//Here, I'm stuck

);

<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>












share|improve this question























  • Note if view is your <select> element then view.value is going to be some word string not an integer which is what splice() expects the first argument to be. This is going to therefor end up effectively being splice(0) which removes all elements from the original array
    – Patrick Evans
    1 hour ago















up vote
6
down vote

favorite












I'm working on a project on data mapping. Several checks are realized:



  • well imported file


  • table choice


  • columns choice of the table


  • typage of data


I'm for at the part of the choice of columns for the moment. I'm stocking these various choices in an array. The problem is that if I want to delete one choice in my array, all data are deleted !
I'm using this plugin: http://wenzhixin.net.cn/p/multiple-select/docs/



Edit: I'm trying now to make an event when all the options are checked by either clicking the “Select all” checkbox, or when the “checkall” method is programatically called. The problem is that I don't see how to get all these checkbox content






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;
);

var contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
,
onCheckAll: function()

//Here, I'm stuck

);

<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>












share|improve this question























  • Note if view is your <select> element then view.value is going to be some word string not an integer which is what splice() expects the first argument to be. This is going to therefor end up effectively being splice(0) which removes all elements from the original array
    – Patrick Evans
    1 hour ago













up vote
6
down vote

favorite









up vote
6
down vote

favorite











I'm working on a project on data mapping. Several checks are realized:



  • well imported file


  • table choice


  • columns choice of the table


  • typage of data


I'm for at the part of the choice of columns for the moment. I'm stocking these various choices in an array. The problem is that if I want to delete one choice in my array, all data are deleted !
I'm using this plugin: http://wenzhixin.net.cn/p/multiple-select/docs/



Edit: I'm trying now to make an event when all the options are checked by either clicking the “Select all” checkbox, or when the “checkall” method is programatically called. The problem is that I don't see how to get all these checkbox content






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;
);

var contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
,
onCheckAll: function()

//Here, I'm stuck

);

<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>












share|improve this question















I'm working on a project on data mapping. Several checks are realized:



  • well imported file


  • table choice


  • columns choice of the table


  • typage of data


I'm for at the part of the choice of columns for the moment. I'm stocking these various choices in an array. The problem is that if I want to delete one choice in my array, all data are deleted !
I'm using this plugin: http://wenzhixin.net.cn/p/multiple-select/docs/



Edit: I'm trying now to make an event when all the options are checked by either clicking the “Select all” checkbox, or when the “checkall” method is programatically called. The problem is that I don't see how to get all these checkbox content






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;
);

var contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
,
onCheckAll: function()

//Here, I'm stuck

);

<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>








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;
);

var contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
,
onCheckAll: function()

//Here, I'm stuck

);

<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>





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;
);

var contentChoiceFields = choiceFields;
console.log(contentChoiceFields);
,
onCheckAll: function()

//Here, I'm stuck

);

<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>






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 45 mins ago

























asked 1 hour ago









Maestro

558




558











  • Note if view is your <select> element then view.value is going to be some word string not an integer which is what splice() expects the first argument to be. This is going to therefor end up effectively being splice(0) which removes all elements from the original array
    – Patrick Evans
    1 hour ago

















  • Note if view is your <select> element then view.value is going to be some word string not an integer which is what splice() expects the first argument to be. This is going to therefor end up effectively being splice(0) which removes all elements from the original array
    – Patrick Evans
    1 hour ago
















Note if view is your <select> element then view.value is going to be some word string not an integer which is what splice() expects the first argument to be. This is going to therefor end up effectively being splice(0) which removes all elements from the original array
– Patrick Evans
1 hour ago





Note if view is your <select> element then view.value is going to be some word string not an integer which is what splice() expects the first argument to be. This is going to therefor end up effectively being splice(0) which removes all elements from the original array
– Patrick Evans
1 hour ago













2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










You can do it like this:



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);

);


I've changed:



choiceFields.splice(view.value);


to:



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


Demo






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>








share|improve this answer




















  • Wow thx a lot @Carsten ! :)
    – Maestro
    1 hour ago










  • No problem @Maestro Happy to help
    – Carsten Løvbo Andersen
    1 hour ago










  • Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
    – Maestro
    59 mins ago

















up vote
1
down vote













In Array.splice() it takes 3 parameters array.splice(index, howMany, [element1][, ..., elementN]) .



index − Index at which to start changing the array.



howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.



element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.



But you had'nt provied howMany parameter in your answer provide this value to delete particular index from array



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);

);

<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>








share|improve this answer






















  • Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
    – Carsten Løvbo Andersen
    1 hour ago










  • @CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
    – Amit chauhan
    1 hour ago










  • Thank u also @Amit!! :)
    – Maestro
    1 hour ago










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52810658%2fsplice-data-in-array%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










You can do it like this:



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);

);


I've changed:



choiceFields.splice(view.value);


to:



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


Demo






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>








share|improve this answer




















  • Wow thx a lot @Carsten ! :)
    – Maestro
    1 hour ago










  • No problem @Maestro Happy to help
    – Carsten Løvbo Andersen
    1 hour ago










  • Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
    – Maestro
    59 mins ago














up vote
6
down vote



accepted










You can do it like this:



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);

);


I've changed:



choiceFields.splice(view.value);


to:



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


Demo






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>








share|improve this answer




















  • Wow thx a lot @Carsten ! :)
    – Maestro
    1 hour ago










  • No problem @Maestro Happy to help
    – Carsten Løvbo Andersen
    1 hour ago










  • Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
    – Maestro
    59 mins ago












up vote
6
down vote



accepted







up vote
6
down vote



accepted






You can do it like this:



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);

);


I've changed:



choiceFields.splice(view.value);


to:



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


Demo






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>








share|improve this answer












You can do it like this:



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);

);


I've changed:



choiceFields.splice(view.value);


to:



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


Demo






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>








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>





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>






share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









Carsten Løvbo Andersen

13.9k72656




13.9k72656











  • Wow thx a lot @Carsten ! :)
    – Maestro
    1 hour ago










  • No problem @Maestro Happy to help
    – Carsten Løvbo Andersen
    1 hour ago










  • Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
    – Maestro
    59 mins ago
















  • Wow thx a lot @Carsten ! :)
    – Maestro
    1 hour ago










  • No problem @Maestro Happy to help
    – Carsten Løvbo Andersen
    1 hour ago










  • Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
    – Maestro
    59 mins ago















Wow thx a lot @Carsten ! :)
– Maestro
1 hour ago




Wow thx a lot @Carsten ! :)
– Maestro
1 hour ago












No problem @Maestro Happy to help
– Carsten Løvbo Andersen
1 hour ago




No problem @Maestro Happy to help
– Carsten Løvbo Andersen
1 hour ago












Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
– Maestro
59 mins ago




Do you have an idea of how I can manage to get all values pushed in array if select all checkbox is checked @Carsten? I tried to add one more function (onCheckAll), specified in documentation, but it's returning me that view is undefined !
– Maestro
59 mins ago












up vote
1
down vote













In Array.splice() it takes 3 parameters array.splice(index, howMany, [element1][, ..., elementN]) .



index − Index at which to start changing the array.



howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.



element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.



But you had'nt provied howMany parameter in your answer provide this value to delete particular index from array



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);

);

<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>








share|improve this answer






















  • Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
    – Carsten Løvbo Andersen
    1 hour ago










  • @CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
    – Amit chauhan
    1 hour ago










  • Thank u also @Amit!! :)
    – Maestro
    1 hour ago














up vote
1
down vote













In Array.splice() it takes 3 parameters array.splice(index, howMany, [element1][, ..., elementN]) .



index − Index at which to start changing the array.



howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.



element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.



But you had'nt provied howMany parameter in your answer provide this value to delete particular index from array



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);

);

<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>








share|improve this answer






















  • Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
    – Carsten Løvbo Andersen
    1 hour ago










  • @CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
    – Amit chauhan
    1 hour ago










  • Thank u also @Amit!! :)
    – Maestro
    1 hour ago












up vote
1
down vote










up vote
1
down vote









In Array.splice() it takes 3 parameters array.splice(index, howMany, [element1][, ..., elementN]) .



index − Index at which to start changing the array.



howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.



element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.



But you had'nt provied howMany parameter in your answer provide this value to delete particular index from array



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);

);

<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>








share|improve this answer














In Array.splice() it takes 3 parameters array.splice(index, howMany, [element1][, ..., elementN]) .



index − Index at which to start changing the array.



howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.



element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.



But you had'nt provied howMany parameter in your answer provide this value to delete particular index from array



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);

);

<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>








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);

);

<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>





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);

);

<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>






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









Amit chauhan

472213




472213











  • Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
    – Carsten Løvbo Andersen
    1 hour ago










  • @CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
    – Amit chauhan
    1 hour ago










  • Thank u also @Amit!! :)
    – Maestro
    1 hour ago
















  • Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
    – Carsten Løvbo Andersen
    1 hour ago










  • @CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
    – Amit chauhan
    1 hour ago










  • Thank u also @Amit!! :)
    – Maestro
    1 hour ago















Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
– Carsten Løvbo Andersen
1 hour ago




Not sure it working correct, If i select Id, then Username and remove Username, then I get 2 times Username in the array
– Carsten Løvbo Andersen
1 hour ago












@CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
– Amit chauhan
1 hour ago




@CarstenLøvboAndersen i've updated the answer now the requirement is acheived using array.splice()
– Amit chauhan
1 hour ago












Thank u also @Amit!! :)
– Maestro
1 hour ago




Thank u also @Amit!! :)
– Maestro
1 hour ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52810658%2fsplice-data-in-array%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery