+ 2
Push data to empty Array
I have a input filed and a button. I want to insert data and click submit i want that information to be pushed to an empty array .Then we console.log that Array we can see all the data which was inserted to that.I HAVE SOME KIND OF ERROR PLEASE CHECK MY CODE I WILL ATTACH THE LINK BELOW https://code.sololearn.com/WJTKrSo3gGW6/?ref=app
15 Respuestas
+ 7
Abdulaziz Abdurasul In your code myArray is only accessible inside your function. When your function execution ends the array is garbage collected and dies. When you click the button and call the function again you just create a new array (everything pushed before has gone). When you do what EmmanueLZ. said you store your array in a global variable that is accessible in the whole code.
myArr.push = something is wrong because you are setting a push property to your array and you won't be able to call the Array.prototype.push method via prototype chaining(you'll have to do it manually).
https://code.sololearn.com/WzEUf1uYbYb4/?ref=app
Sorry for my delay.
+ 6
You can use es6 spread Syntex its easy use and easy to understand
a=[]
b = [1,2,3]
a=[...b]
That's it
https://www.sololearn.com/learn/JavaScript/2978/
here a simple example
Note: I will delete this demo code in 24h , please copy if you want.
https://code.sololearn.com/Wa5LhoU297QM/?ref=app
+ 5
What kind error. I can't see any.
My code fits your question description and also works.
https://code.sololearn.com/Wd7wLMpF8GT6/?ref=app
Can you share your input and expected output?
+ 5
Abdulaziz Abdurasul hi,
Shouldn't you declare your array outside the function.
As it is, I think each time you click your array is overwritten.
May someone correct if I'm wrong...
+ 3
EmmanueLZ. And right now i also got one thing that, if an array inside the function we can not have acces to it because of function scope ,and ones we put it outside of function we can have access to it to print whatever we have inside of it
+ 2
function myFunction(){
var myArr = [];
var x;
for(x of document.getElementById('text').value.split(" "))
myArr.push(x);
document.write(myArr);
}
+ 2
Kevin Star Bro but why whenever we input new value it store it in new array ?
+ 2
EmmanueLZ. YEAHHH BROTHER THATS WORKS))) BUT CAN YOU EXPLAIN WHY IT WORKS LIKE THIS ?🙏🙏
+ 2
Also in your html for the onclick="myFunction()" there is a ";" just after parenthesis maybe it can confuse when the browser run the prog.
And also not sure, but the way you linked your :
myArr.push =document.getElementById('text').value , is it accepted , maybe it's better , as mentioned by rodwynnejones , to declare a variable to welcome your value and then use myArr.push(x)
+ 2
EmmanueLZ. Yaa i got this but o just want to know why we have to put our array outside of the function thats interesting 🤔
+ 2
Good for you.
Why it works that way? I'm not enough advanced to give full information,but if it is inside the function it's like you start that variable as if it is the first time.
+ 2
That's right.
+ 2
Indeed the syntax of Kevin Star is better when it come to push your text.value
+ 2
Check on line 4 of your code.
try using push as a method not as a variable. Or because you assign myArr.push as a variable, correct your console.log(myArr) and make console.log(myArr.push).
your code will work properly.
Just take a look on this two solutions
https://code.sololearn.com/WMeNg00nfEhF/?ref=app
https://code.sololearn.com/WAN8w512AG5C/?ref=app
+ 1
How to add website in backend