Delete all elements in an array - Function appends 'undefined' at the array end
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Here is my code
var x = ;
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
I simply wanna remove all the elements in the array then add new elements in it.
But when I try to do it with the code above, undefined
is also adding to the array.
How can I prevent it?
javascript arrays push splice
|
show 2 more comments
Here is my code
var x = ;
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
I simply wanna remove all the elements in the array then add new elements in it.
But when I try to do it with the code above, undefined
is also adding to the array.
How can I prevent it?
javascript arrays push splice
4
why not just reset it withx =
?
– chazsolo
Nov 16 '18 at 13:16
3
so, what aboutx = ;
instead of splicing and whatever?
– briosheje
Nov 16 '18 at 13:16
3
orx.length = 0;
– Nina Scholz
Nov 16 '18 at 13:17
1
Why don't you just assign empty array to it? 'x='
– Slobodan Gajić
Nov 16 '18 at 13:17
2
Possible duplicate of How do I empty an array in JavaScript?
– kemicofa
Nov 16 '18 at 13:21
|
show 2 more comments
Here is my code
var x = ;
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
I simply wanna remove all the elements in the array then add new elements in it.
But when I try to do it with the code above, undefined
is also adding to the array.
How can I prevent it?
javascript arrays push splice
Here is my code
var x = ;
function random(min,max) {
return Math.floor(Math.random() * (min-max))+min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0,b));
}
}
random2(5, 100);
console.log(x); // [ -43, -27, -38, -21, -79 ]
x.splice(0, x.length);
x.push(random2(5,100));
console.log(x); // [ -24, -97, -99, -43, -66, undefined ]
I simply wanna remove all the elements in the array then add new elements in it.
But when I try to do it with the code above, undefined
is also adding to the array.
How can I prevent it?
javascript arrays push splice
javascript arrays push splice
edited Nov 16 '18 at 13:36
Vignesh Raja
3,02211121
3,02211121
asked Nov 16 '18 at 13:15
uhbcuhbc
526
526
4
why not just reset it withx =
?
– chazsolo
Nov 16 '18 at 13:16
3
so, what aboutx = ;
instead of splicing and whatever?
– briosheje
Nov 16 '18 at 13:16
3
orx.length = 0;
– Nina Scholz
Nov 16 '18 at 13:17
1
Why don't you just assign empty array to it? 'x='
– Slobodan Gajić
Nov 16 '18 at 13:17
2
Possible duplicate of How do I empty an array in JavaScript?
– kemicofa
Nov 16 '18 at 13:21
|
show 2 more comments
4
why not just reset it withx =
?
– chazsolo
Nov 16 '18 at 13:16
3
so, what aboutx = ;
instead of splicing and whatever?
– briosheje
Nov 16 '18 at 13:16
3
orx.length = 0;
– Nina Scholz
Nov 16 '18 at 13:17
1
Why don't you just assign empty array to it? 'x='
– Slobodan Gajić
Nov 16 '18 at 13:17
2
Possible duplicate of How do I empty an array in JavaScript?
– kemicofa
Nov 16 '18 at 13:21
4
4
why not just reset it with
x =
?– chazsolo
Nov 16 '18 at 13:16
why not just reset it with
x =
?– chazsolo
Nov 16 '18 at 13:16
3
3
so, what about
x = ;
instead of splicing and whatever?– briosheje
Nov 16 '18 at 13:16
so, what about
x = ;
instead of splicing and whatever?– briosheje
Nov 16 '18 at 13:16
3
3
or
x.length = 0;
– Nina Scholz
Nov 16 '18 at 13:17
or
x.length = 0;
– Nina Scholz
Nov 16 '18 at 13:17
1
1
Why don't you just assign empty array to it? 'x='
– Slobodan Gajić
Nov 16 '18 at 13:17
Why don't you just assign empty array to it? 'x='
– Slobodan Gajić
Nov 16 '18 at 13:17
2
2
Possible duplicate of How do I empty an array in JavaScript?
– kemicofa
Nov 16 '18 at 13:21
Possible duplicate of How do I empty an array in JavaScript?
– kemicofa
Nov 16 '18 at 13:21
|
show 2 more comments
3 Answers
3
active
oldest
votes
You need not to puish the function call, which returns undefined
, but just call the function random2
, because the function itselft add the elements to the array.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
A better approach is to return an array in random2
, because this function does not access an outer defined array. To push the values, you could take the spread syntax.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
@nina, Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming the OP need to createmin
number of random numbers betweenmin
andmax
values ? Even the OP is wrong in his implementation.
– Vignesh Raja
Nov 16 '18 at 13:40
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
add a comment |
To empty an array, there are multiple ways as explained here with some benchmark results and explanation regarding their performance.
As an aggregation, asssume var a = [1,2,3,4,5]
a =
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
You have called the function random2
inside the push method. So random2
method first inserts the values in the array x
and returns the default value undefined
(Reference), which in turn gets pushed into the array. Hence the value.
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
1
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
@uhbc Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming you need to createmin
number of random numbers betweenmin
andmax
values ?
– Vignesh Raja
Nov 16 '18 at 13:44
add a comment |
Set the length to zero
x.length = 0;
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%2f53338660%2fdelete-all-elements-in-an-array-function-appends-undefined-at-the-array-end%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need not to puish the function call, which returns undefined
, but just call the function random2
, because the function itselft add the elements to the array.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
A better approach is to return an array in random2
, because this function does not access an outer defined array. To push the values, you could take the spread syntax.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
@nina, Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming the OP need to createmin
number of random numbers betweenmin
andmax
values ? Even the OP is wrong in his implementation.
– Vignesh Raja
Nov 16 '18 at 13:40
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
add a comment |
You need not to puish the function call, which returns undefined
, but just call the function random2
, because the function itselft add the elements to the array.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
A better approach is to return an array in random2
, because this function does not access an outer defined array. To push the values, you could take the spread syntax.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
@nina, Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming the OP need to createmin
number of random numbers betweenmin
andmax
values ? Even the OP is wrong in his implementation.
– Vignesh Raja
Nov 16 '18 at 13:40
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
add a comment |
You need not to puish the function call, which returns undefined
, but just call the function random2
, because the function itselft add the elements to the array.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
A better approach is to return an array in random2
, because this function does not access an outer defined array. To push the values, you could take the spread syntax.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
You need not to puish the function call, which returns undefined
, but just call the function random2
, because the function itselft add the elements to the array.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
A better approach is to return an array in random2
, because this function does not access an outer defined array. To push the values, you could take the spread syntax.
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
for (let i = 0; i < a; i++) {
x.push(random(0, b));
}
}
var x = ;
random2(5, 100);
console.log(x);
x.length = 0; // better performance than x.splice(0, x.length)
random2(5,100); // call without using push
console.log(x); // no undefined anymore
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
function random(min, max) {
return Math.floor(Math.random() * (min - max)) + min;
}
function random2(a, b) {
return Array.from({ length: a }, _ => random(0, b));
}
var x = random2(5, 100);
console.log(x);
x.length = 0;
x.push(...random2(5, 100));
console.log(x);
edited Nov 16 '18 at 13:30
answered Nov 16 '18 at 13:22
Nina ScholzNina Scholz
196k15109179
196k15109179
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
@nina, Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming the OP need to createmin
number of random numbers betweenmin
andmax
values ? Even the OP is wrong in his implementation.
– Vignesh Raja
Nov 16 '18 at 13:40
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
add a comment |
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
@nina, Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming the OP need to createmin
number of random numbers betweenmin
andmax
values ? Even the OP is wrong in his implementation.
– Vignesh Raja
Nov 16 '18 at 13:40
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
After 3h of sleep haven't thought of it, thanks
– uhbc
Nov 16 '18 at 13:26
@nina, Out of the context, isn't it should be
random(a,b)
inside random2
method, assuming the OP need to create min
number of random numbers between min
and max
values ? Even the OP is wrong in his implementation.– Vignesh Raja
Nov 16 '18 at 13:40
@nina, Out of the context, isn't it should be
random(a,b)
inside random2
method, assuming the OP need to create min
number of random numbers between min
and max
values ? Even the OP is wrong in his implementation.– Vignesh Raja
Nov 16 '18 at 13:40
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
@VigneshRaja, please ask op.
– Nina Scholz
Nov 16 '18 at 13:42
add a comment |
To empty an array, there are multiple ways as explained here with some benchmark results and explanation regarding their performance.
As an aggregation, asssume var a = [1,2,3,4,5]
a =
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
You have called the function random2
inside the push method. So random2
method first inserts the values in the array x
and returns the default value undefined
(Reference), which in turn gets pushed into the array. Hence the value.
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
1
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
@uhbc Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming you need to createmin
number of random numbers betweenmin
andmax
values ?
– Vignesh Raja
Nov 16 '18 at 13:44
add a comment |
To empty an array, there are multiple ways as explained here with some benchmark results and explanation regarding their performance.
As an aggregation, asssume var a = [1,2,3,4,5]
a =
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
You have called the function random2
inside the push method. So random2
method first inserts the values in the array x
and returns the default value undefined
(Reference), which in turn gets pushed into the array. Hence the value.
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
1
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
@uhbc Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming you need to createmin
number of random numbers betweenmin
andmax
values ?
– Vignesh Raja
Nov 16 '18 at 13:44
add a comment |
To empty an array, there are multiple ways as explained here with some benchmark results and explanation regarding their performance.
As an aggregation, asssume var a = [1,2,3,4,5]
a =
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
You have called the function random2
inside the push method. So random2
method first inserts the values in the array x
and returns the default value undefined
(Reference), which in turn gets pushed into the array. Hence the value.
To empty an array, there are multiple ways as explained here with some benchmark results and explanation regarding their performance.
As an aggregation, asssume var a = [1,2,3,4,5]
a =
a.length = 0
a.splice(0, a.length)
a = new Array()
while(a.pop()){}
while(a.shift()){}
You have called the function random2
inside the push method. So random2
method first inserts the values in the array x
and returns the default value undefined
(Reference), which in turn gets pushed into the array. Hence the value.
answered Nov 16 '18 at 13:28
Vignesh RajaVignesh Raja
3,02211121
3,02211121
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
1
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
@uhbc Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming you need to createmin
number of random numbers betweenmin
andmax
values ?
– Vignesh Raja
Nov 16 '18 at 13:44
add a comment |
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
1
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
@uhbc Out of the context, isn't it should berandom(a,b)
insiderandom2
method, assuming you need to createmin
number of random numbers betweenmin
andmax
values ?
– Vignesh Raja
Nov 16 '18 at 13:44
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
Thank you. That's correct, couldn't realise that first but thank to Nina, helped me realise that.
– uhbc
Nov 16 '18 at 13:33
1
1
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
Glad you got it. :)
– Vignesh Raja
Nov 16 '18 at 13:34
@uhbc Out of the context, isn't it should be
random(a,b)
inside random2
method, assuming you need to create min
number of random numbers between min
and max
values ?– Vignesh Raja
Nov 16 '18 at 13:44
@uhbc Out of the context, isn't it should be
random(a,b)
inside random2
method, assuming you need to create min
number of random numbers between min
and max
values ?– Vignesh Raja
Nov 16 '18 at 13:44
add a comment |
Set the length to zero
x.length = 0;
add a comment |
Set the length to zero
x.length = 0;
add a comment |
Set the length to zero
x.length = 0;
Set the length to zero
x.length = 0;
answered Nov 16 '18 at 13:17
Josef FazekasJosef Fazekas
30037
30037
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%2f53338660%2fdelete-all-elements-in-an-array-function-appends-undefined-at-the-array-end%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
4
why not just reset it with
x =
?– chazsolo
Nov 16 '18 at 13:16
3
so, what about
x = ;
instead of splicing and whatever?– briosheje
Nov 16 '18 at 13:16
3
or
x.length = 0;
– Nina Scholz
Nov 16 '18 at 13:17
1
Why don't you just assign empty array to it? 'x='
– Slobodan Gajić
Nov 16 '18 at 13:17
2
Possible duplicate of How do I empty an array in JavaScript?
– kemicofa
Nov 16 '18 at 13:21