How add and remove array without re-index array key in JQuery
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??
javascript jquery html arrays
add a comment |
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??
javascript jquery html arrays
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 ofnew_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
add a comment |
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??
javascript jquery html arrays
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
javascript jquery html arrays
asked Nov 14 '18 at 9:02
Praveen KumarPraveen Kumar
389315
389315
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 ofnew_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
add a comment |
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 ofnew_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
add a comment |
2 Answers
2
active
oldest
votes
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];
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
|
show 4 more comments
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);
);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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];
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
|
show 4 more comments
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];
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
|
show 4 more comments
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];
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>
edited Dec 4 '18 at 18:07
answered Nov 14 '18 at 9:05
Nikhil AggarwalNikhil Aggarwal
24.1k32748
24.1k32748
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
|
show 4 more comments
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
|
show 4 more comments
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);
);
add a comment |
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);
);
add a comment |
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);
);
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);
);
answered Nov 14 '18 at 9:07
Ramas WinRamas Win
71
71
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 ofnew_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