How add and remove array without re-index array key in JQuery












4















I have a list of the checkbox when clicking on any checkbox than new array append (push) in the main array. If uncheck than remove but not change any index for the current array.



explain:-



Like:- When I click on the first checkbox than array like



0: ["2"]


Like:- When I click on the second checkbox than array like



0: ["2"]
1: ["3"]


Like :- When I click on four checkbox than array like



0: ["2"]
1: ["3"]
2: ["5"]


after than uncheck checkbox if I uncheck first tthe han I needed array



1: ["3"]
2: ["5"]


again I click on first checkbox than need array link



1: ["3"]
2: ["5"]
3: ["2"]


Not need to change any array index key



https://jsfiddle.net/tx63yjhg/






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = ;

$("input").on("change", function()
{
var $this = $(this);

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
else
{
//remove array when uncheck checkbox
}
console.log('new_value',new_value);
});
</script>





How I can remove array and add again??










share|improve this question























  • Why are you having array of arrays? Why not a simple flattened array instead?

    – Nikhil Aggarwal
    Nov 14 '18 at 9:04











  • @NikhilAggarwal :- I have multiple values in the array so I need array inside array

    – Praveen Kumar
    Nov 14 '18 at 9:05






  • 2





    Okay. Can you post the realistic array then. In the example posted above after removing value you expect array to be, 1: ["3"], 2: ["5"]. What shall be the value of new_value[0]

    – Nikhil Aggarwal
    Nov 14 '18 at 9:10











  • @NikhilAggarwal : If remove 0 index than 0 index is removed and this key does not assign any array index

    – Praveen Kumar
    Nov 14 '18 at 9:13











  • Look at this example - let arr = [1,2]; delete arr[0]; console.log(arr);. See the array index is still there with an empty value.

    – Nikhil Aggarwal
    Nov 14 '18 at 9:15
















4















I have a list of the checkbox when clicking on any checkbox than new array append (push) in the main array. If uncheck than remove but not change any index for the current array.



explain:-



Like:- When I click on the first checkbox than array like



0: ["2"]


Like:- When I click on the second checkbox than array like



0: ["2"]
1: ["3"]


Like :- When I click on four checkbox than array like



0: ["2"]
1: ["3"]
2: ["5"]


after than uncheck checkbox if I uncheck first tthe han I needed array



1: ["3"]
2: ["5"]


again I click on first checkbox than need array link



1: ["3"]
2: ["5"]
3: ["2"]


Not need to change any array index key



https://jsfiddle.net/tx63yjhg/






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = ;

$("input").on("change", function()
{
var $this = $(this);

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
else
{
//remove array when uncheck checkbox
}
console.log('new_value',new_value);
});
</script>





How I can remove array and add again??










share|improve this question























  • Why are you having array of arrays? Why not a simple flattened array instead?

    – Nikhil Aggarwal
    Nov 14 '18 at 9:04











  • @NikhilAggarwal :- I have multiple values in the array so I need array inside array

    – Praveen Kumar
    Nov 14 '18 at 9:05






  • 2





    Okay. Can you post the realistic array then. In the example posted above after removing value you expect array to be, 1: ["3"], 2: ["5"]. What shall be the value of new_value[0]

    – Nikhil Aggarwal
    Nov 14 '18 at 9:10











  • @NikhilAggarwal : If remove 0 index than 0 index is removed and this key does not assign any array index

    – Praveen Kumar
    Nov 14 '18 at 9:13











  • Look at this example - let arr = [1,2]; delete arr[0]; console.log(arr);. See the array index is still there with an empty value.

    – Nikhil Aggarwal
    Nov 14 '18 at 9:15














4












4








4








I have a list of the checkbox when clicking on any checkbox than new array append (push) in the main array. If uncheck than remove but not change any index for the current array.



explain:-



Like:- When I click on the first checkbox than array like



0: ["2"]


Like:- When I click on the second checkbox than array like



0: ["2"]
1: ["3"]


Like :- When I click on four checkbox than array like



0: ["2"]
1: ["3"]
2: ["5"]


after than uncheck checkbox if I uncheck first tthe han I needed array



1: ["3"]
2: ["5"]


again I click on first checkbox than need array link



1: ["3"]
2: ["5"]
3: ["2"]


Not need to change any array index key



https://jsfiddle.net/tx63yjhg/






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = ;

$("input").on("change", function()
{
var $this = $(this);

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
else
{
//remove array when uncheck checkbox
}
console.log('new_value',new_value);
});
</script>





How I can remove array and add again??










share|improve this question














I have a list of the checkbox when clicking on any checkbox than new array append (push) in the main array. If uncheck than remove but not change any index for the current array.



explain:-



Like:- When I click on the first checkbox than array like



0: ["2"]


Like:- When I click on the second checkbox than array like



0: ["2"]
1: ["3"]


Like :- When I click on four checkbox than array like



0: ["2"]
1: ["3"]
2: ["5"]


after than uncheck checkbox if I uncheck first tthe han I needed array



1: ["3"]
2: ["5"]


again I click on first checkbox than need array link



1: ["3"]
2: ["5"]
3: ["2"]


Not need to change any array index key



https://jsfiddle.net/tx63yjhg/






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = ;

$("input").on("change", function()
{
var $this = $(this);

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
else
{
//remove array when uncheck checkbox
}
console.log('new_value',new_value);
});
</script>





How I can remove array and add again??






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = ;

$("input").on("change", function()
{
var $this = $(this);

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
else
{
//remove array when uncheck checkbox
}
console.log('new_value',new_value);
});
</script>





<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = ;

$("input").on("change", function()
{
var $this = $(this);

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
else
{
//remove array when uncheck checkbox
}
console.log('new_value',new_value);
});
</script>






javascript jquery html arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:02









Praveen KumarPraveen Kumar

369313




369313













  • Why are you having array of arrays? Why not a simple flattened array instead?

    – Nikhil Aggarwal
    Nov 14 '18 at 9:04











  • @NikhilAggarwal :- I have multiple values in the array so I need array inside array

    – Praveen Kumar
    Nov 14 '18 at 9:05






  • 2





    Okay. Can you post the realistic array then. In the example posted above after removing value you expect array to be, 1: ["3"], 2: ["5"]. What shall be the value of new_value[0]

    – Nikhil Aggarwal
    Nov 14 '18 at 9:10











  • @NikhilAggarwal : If remove 0 index than 0 index is removed and this key does not assign any array index

    – Praveen Kumar
    Nov 14 '18 at 9:13











  • Look at this example - let arr = [1,2]; delete arr[0]; console.log(arr);. See the array index is still there with an empty value.

    – Nikhil Aggarwal
    Nov 14 '18 at 9:15



















  • Why are you having array of arrays? Why not a simple flattened array instead?

    – Nikhil Aggarwal
    Nov 14 '18 at 9:04











  • @NikhilAggarwal :- I have multiple values in the array so I need array inside array

    – Praveen Kumar
    Nov 14 '18 at 9:05






  • 2





    Okay. Can you post the realistic array then. In the example posted above after removing value you expect array to be, 1: ["3"], 2: ["5"]. What shall be the value of new_value[0]

    – Nikhil Aggarwal
    Nov 14 '18 at 9:10











  • @NikhilAggarwal : If remove 0 index than 0 index is removed and this key does not assign any array index

    – Praveen Kumar
    Nov 14 '18 at 9:13











  • Look at this example - let arr = [1,2]; delete arr[0]; console.log(arr);. See the array index is still there with an empty value.

    – Nikhil Aggarwal
    Nov 14 '18 at 9:15

















Why are you having array of arrays? Why not a simple flattened array instead?

– Nikhil Aggarwal
Nov 14 '18 at 9:04





Why are you having array of arrays? Why not a simple flattened array instead?

– Nikhil Aggarwal
Nov 14 '18 at 9:04













@NikhilAggarwal :- I have multiple values in the array so I need array inside array

– Praveen Kumar
Nov 14 '18 at 9:05





@NikhilAggarwal :- I have multiple values in the array so I need array inside array

– Praveen Kumar
Nov 14 '18 at 9:05




2




2





Okay. Can you post the realistic array then. In the example posted above after removing value you expect array to be, 1: ["3"], 2: ["5"]. What shall be the value of new_value[0]

– Nikhil Aggarwal
Nov 14 '18 at 9:10





Okay. Can you post the realistic array then. In the example posted above after removing value you expect array to be, 1: ["3"], 2: ["5"]. What shall be the value of new_value[0]

– Nikhil Aggarwal
Nov 14 '18 at 9:10













@NikhilAggarwal : If remove 0 index than 0 index is removed and this key does not assign any array index

– Praveen Kumar
Nov 14 '18 at 9:13





@NikhilAggarwal : If remove 0 index than 0 index is removed and this key does not assign any array index

– Praveen Kumar
Nov 14 '18 at 9:13













Look at this example - let arr = [1,2]; delete arr[0]; console.log(arr);. See the array index is still there with an empty value.

– Nikhil Aggarwal
Nov 14 '18 at 9:15





Look at this example - let arr = [1,2]; delete arr[0]; console.log(arr);. See the array index is still there with an empty value.

– Nikhil Aggarwal
Nov 14 '18 at 9:15












2 Answers
2






active

oldest

votes


















1














I am not sure whether you achieve the same using an array of arrays or not. You can instead try use an object like following.






<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" />
<input type="checkbox" value="6" />
<input type="checkbox" value="7" />

<script type="text/javascript">
var values = ;
var new_value = {};
var counter = 0;

$("input").on("change", function() {
var $this = $(this);

if ($this.is(":checked")) {
new_value[counter++] = [$this.val()];
} else {
Object.entries(new_value).forEach(([k,v]) => {
if(v.includes($this.val())) delete new_value[k];
});
}
console.log('new_value',new_value);
});
</script>





EDIT



For add



new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":474}; // e.g. object


For remove



let valueToBeRemoved = 234; // e.g. value to be removed

Object.entries(new_value).forEach(([k,v]) => {
if(v.value === valueToBeRemoved) delete new_value[k];
})


EDIT 2



for (var k in new_value) {
if(new_value[k].value === valueToBeRemoved) delete new_value[k];
}





share|improve this answer


























  • If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

    – Praveen Kumar
    Nov 14 '18 at 10:11













  • @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

    – Nikhil Aggarwal
    Nov 14 '18 at 10:14






  • 1





    @PraveenKumar - I have updated my answer with add / edit block. Can you please check

    – Nikhil Aggarwal
    Nov 14 '18 at 10:22






  • 1





    Nikhil Aggarwal :- Thank you for the update answer

    – Praveen Kumar
    Nov 14 '18 at 10:25








  • 1





    @PraveenKumar - Glad to help you :)

    – Nikhil Aggarwal
    Nov 14 '18 at 10:26



















-1














It easy:






var values = ;

$("input").on("change", function()
{
var $this = $(this);
var new_value = ;

if ($this.is(":checked"))
{
var new_data = [$this.val()];
new_value.push(new_data);
}
console.log('new_value',new_value);
});








share|improve this answer























    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    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%2f53296364%2fhow-add-and-remove-array-without-re-index-array-key-in-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I am not sure whether you achieve the same using an array of arrays or not. You can instead try use an object like following.






    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />
    <input type="checkbox" value="2" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="4" />
    <input type="checkbox" value="5" />
    <input type="checkbox" value="6" />
    <input type="checkbox" value="7" />

    <script type="text/javascript">
    var values = ;
    var new_value = {};
    var counter = 0;

    $("input").on("change", function() {
    var $this = $(this);

    if ($this.is(":checked")) {
    new_value[counter++] = [$this.val()];
    } else {
    Object.entries(new_value).forEach(([k,v]) => {
    if(v.includes($this.val())) delete new_value[k];
    });
    }
    console.log('new_value',new_value);
    });
    </script>





    EDIT



    For add



    new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":474}; // e.g. object


    For remove



    let valueToBeRemoved = 234; // e.g. value to be removed

    Object.entries(new_value).forEach(([k,v]) => {
    if(v.value === valueToBeRemoved) delete new_value[k];
    })


    EDIT 2



    for (var k in new_value) {
    if(new_value[k].value === valueToBeRemoved) delete new_value[k];
    }





    share|improve this answer


























    • If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

      – Praveen Kumar
      Nov 14 '18 at 10:11













    • @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

      – Nikhil Aggarwal
      Nov 14 '18 at 10:14






    • 1





      @PraveenKumar - I have updated my answer with add / edit block. Can you please check

      – Nikhil Aggarwal
      Nov 14 '18 at 10:22






    • 1





      Nikhil Aggarwal :- Thank you for the update answer

      – Praveen Kumar
      Nov 14 '18 at 10:25








    • 1





      @PraveenKumar - Glad to help you :)

      – Nikhil Aggarwal
      Nov 14 '18 at 10:26
















    1














    I am not sure whether you achieve the same using an array of arrays or not. You can instead try use an object like following.






    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />
    <input type="checkbox" value="2" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="4" />
    <input type="checkbox" value="5" />
    <input type="checkbox" value="6" />
    <input type="checkbox" value="7" />

    <script type="text/javascript">
    var values = ;
    var new_value = {};
    var counter = 0;

    $("input").on("change", function() {
    var $this = $(this);

    if ($this.is(":checked")) {
    new_value[counter++] = [$this.val()];
    } else {
    Object.entries(new_value).forEach(([k,v]) => {
    if(v.includes($this.val())) delete new_value[k];
    });
    }
    console.log('new_value',new_value);
    });
    </script>





    EDIT



    For add



    new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":474}; // e.g. object


    For remove



    let valueToBeRemoved = 234; // e.g. value to be removed

    Object.entries(new_value).forEach(([k,v]) => {
    if(v.value === valueToBeRemoved) delete new_value[k];
    })


    EDIT 2



    for (var k in new_value) {
    if(new_value[k].value === valueToBeRemoved) delete new_value[k];
    }





    share|improve this answer


























    • If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

      – Praveen Kumar
      Nov 14 '18 at 10:11













    • @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

      – Nikhil Aggarwal
      Nov 14 '18 at 10:14






    • 1





      @PraveenKumar - I have updated my answer with add / edit block. Can you please check

      – Nikhil Aggarwal
      Nov 14 '18 at 10:22






    • 1





      Nikhil Aggarwal :- Thank you for the update answer

      – Praveen Kumar
      Nov 14 '18 at 10:25








    • 1





      @PraveenKumar - Glad to help you :)

      – Nikhil Aggarwal
      Nov 14 '18 at 10:26














    1












    1








    1







    I am not sure whether you achieve the same using an array of arrays or not. You can instead try use an object like following.






    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />
    <input type="checkbox" value="2" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="4" />
    <input type="checkbox" value="5" />
    <input type="checkbox" value="6" />
    <input type="checkbox" value="7" />

    <script type="text/javascript">
    var values = ;
    var new_value = {};
    var counter = 0;

    $("input").on("change", function() {
    var $this = $(this);

    if ($this.is(":checked")) {
    new_value[counter++] = [$this.val()];
    } else {
    Object.entries(new_value).forEach(([k,v]) => {
    if(v.includes($this.val())) delete new_value[k];
    });
    }
    console.log('new_value',new_value);
    });
    </script>





    EDIT



    For add



    new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":474}; // e.g. object


    For remove



    let valueToBeRemoved = 234; // e.g. value to be removed

    Object.entries(new_value).forEach(([k,v]) => {
    if(v.value === valueToBeRemoved) delete new_value[k];
    })


    EDIT 2



    for (var k in new_value) {
    if(new_value[k].value === valueToBeRemoved) delete new_value[k];
    }





    share|improve this answer















    I am not sure whether you achieve the same using an array of arrays or not. You can instead try use an object like following.






    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />
    <input type="checkbox" value="2" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="4" />
    <input type="checkbox" value="5" />
    <input type="checkbox" value="6" />
    <input type="checkbox" value="7" />

    <script type="text/javascript">
    var values = ;
    var new_value = {};
    var counter = 0;

    $("input").on("change", function() {
    var $this = $(this);

    if ($this.is(":checked")) {
    new_value[counter++] = [$this.val()];
    } else {
    Object.entries(new_value).forEach(([k,v]) => {
    if(v.includes($this.val())) delete new_value[k];
    });
    }
    console.log('new_value',new_value);
    });
    </script>





    EDIT



    For add



    new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":474}; // e.g. object


    For remove



    let valueToBeRemoved = 234; // e.g. value to be removed

    Object.entries(new_value).forEach(([k,v]) => {
    if(v.value === valueToBeRemoved) delete new_value[k];
    })


    EDIT 2



    for (var k in new_value) {
    if(new_value[k].value === valueToBeRemoved) delete new_value[k];
    }





    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />
    <input type="checkbox" value="2" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="4" />
    <input type="checkbox" value="5" />
    <input type="checkbox" value="6" />
    <input type="checkbox" value="7" />

    <script type="text/javascript">
    var values = ;
    var new_value = {};
    var counter = 0;

    $("input").on("change", function() {
    var $this = $(this);

    if ($this.is(":checked")) {
    new_value[counter++] = [$this.val()];
    } else {
    Object.entries(new_value).forEach(([k,v]) => {
    if(v.includes($this.val())) delete new_value[k];
    });
    }
    console.log('new_value',new_value);
    });
    </script>





    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />
    <input type="checkbox" value="2" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="4" />
    <input type="checkbox" value="5" />
    <input type="checkbox" value="6" />
    <input type="checkbox" value="7" />

    <script type="text/javascript">
    var values = ;
    var new_value = {};
    var counter = 0;

    $("input").on("change", function() {
    var $this = $(this);

    if ($this.is(":checked")) {
    new_value[counter++] = [$this.val()];
    } else {
    Object.entries(new_value).forEach(([k,v]) => {
    if(v.includes($this.val())) delete new_value[k];
    });
    }
    console.log('new_value',new_value);
    });
    </script>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 4 '18 at 18:07

























    answered Nov 14 '18 at 9:05









    Nikhil AggarwalNikhil Aggarwal

    23.8k32747




    23.8k32747













    • If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

      – Praveen Kumar
      Nov 14 '18 at 10:11













    • @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

      – Nikhil Aggarwal
      Nov 14 '18 at 10:14






    • 1





      @PraveenKumar - I have updated my answer with add / edit block. Can you please check

      – Nikhil Aggarwal
      Nov 14 '18 at 10:22






    • 1





      Nikhil Aggarwal :- Thank you for the update answer

      – Praveen Kumar
      Nov 14 '18 at 10:25








    • 1





      @PraveenKumar - Glad to help you :)

      – Nikhil Aggarwal
      Nov 14 '18 at 10:26



















    • If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

      – Praveen Kumar
      Nov 14 '18 at 10:11













    • @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

      – Nikhil Aggarwal
      Nov 14 '18 at 10:14






    • 1





      @PraveenKumar - I have updated my answer with add / edit block. Can you please check

      – Nikhil Aggarwal
      Nov 14 '18 at 10:22






    • 1





      Nikhil Aggarwal :- Thank you for the update answer

      – Praveen Kumar
      Nov 14 '18 at 10:25








    • 1





      @PraveenKumar - Glad to help you :)

      – Nikhil Aggarwal
      Nov 14 '18 at 10:26

















    If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

    – Praveen Kumar
    Nov 14 '18 at 10:11







    If array value like this than ?? new_value[counter++] = {id1: 100, id2: 200, "id3": 300,"value":value474};

    – Praveen Kumar
    Nov 14 '18 at 10:11















    @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

    – Nikhil Aggarwal
    Nov 14 '18 at 10:14





    @PraveenKumar - You can do it. However, we will need to update our delete code. Which property of the object defines the uniqueness of the object?

    – Nikhil Aggarwal
    Nov 14 '18 at 10:14




    1




    1





    @PraveenKumar - I have updated my answer with add / edit block. Can you please check

    – Nikhil Aggarwal
    Nov 14 '18 at 10:22





    @PraveenKumar - I have updated my answer with add / edit block. Can you please check

    – Nikhil Aggarwal
    Nov 14 '18 at 10:22




    1




    1





    Nikhil Aggarwal :- Thank you for the update answer

    – Praveen Kumar
    Nov 14 '18 at 10:25







    Nikhil Aggarwal :- Thank you for the update answer

    – Praveen Kumar
    Nov 14 '18 at 10:25






    1




    1





    @PraveenKumar - Glad to help you :)

    – Nikhil Aggarwal
    Nov 14 '18 at 10:26





    @PraveenKumar - Glad to help you :)

    – Nikhil Aggarwal
    Nov 14 '18 at 10:26













    -1














    It easy:






    var values = ;

    $("input").on("change", function()
    {
    var $this = $(this);
    var new_value = ;

    if ($this.is(":checked"))
    {
    var new_data = [$this.val()];
    new_value.push(new_data);
    }
    console.log('new_value',new_value);
    });








    share|improve this answer




























      -1














      It easy:






      var values = ;

      $("input").on("change", function()
      {
      var $this = $(this);
      var new_value = ;

      if ($this.is(":checked"))
      {
      var new_data = [$this.val()];
      new_value.push(new_data);
      }
      console.log('new_value',new_value);
      });








      share|improve this answer


























        -1












        -1








        -1







        It easy:






        var values = ;

        $("input").on("change", function()
        {
        var $this = $(this);
        var new_value = ;

        if ($this.is(":checked"))
        {
        var new_data = [$this.val()];
        new_value.push(new_data);
        }
        console.log('new_value',new_value);
        });








        share|improve this answer













        It easy:






        var values = ;

        $("input").on("change", function()
        {
        var $this = $(this);
        var new_value = ;

        if ($this.is(":checked"))
        {
        var new_data = [$this.val()];
        new_value.push(new_data);
        }
        console.log('new_value',new_value);
        });








        var values = ;

        $("input").on("change", function()
        {
        var $this = $(this);
        var new_value = ;

        if ($this.is(":checked"))
        {
        var new_data = [$this.val()];
        new_value.push(new_data);
        }
        console.log('new_value',new_value);
        });





        var values = ;

        $("input").on("change", function()
        {
        var $this = $(this);
        var new_value = ;

        if ($this.is(":checked"))
        {
        var new_data = [$this.val()];
        new_value.push(new_data);
        }
        console.log('new_value',new_value);
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 9:07









        Ramas WinRamas Win

        71




        71






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296364%2fhow-add-and-remove-array-without-re-index-array-key-in-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma