Push data to empty Array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

14th Mar 2020, 10:15 PM
Abdulaziz Abdurasul
Abdulaziz Abdurasul - avatar
15 Answers
+ 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.
15th Mar 2020, 12:46 AM
Kevin ★
+ 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
16th Mar 2020, 3:33 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 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?
14th Mar 2020, 10:40 PM
Kevin ★
+ 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...
14th Mar 2020, 11:05 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 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
14th Mar 2020, 11:21 PM
Abdulaziz Abdurasul
Abdulaziz Abdurasul - avatar
+ 2
function myFunction(){ var myArr = []; var x; for(x of document.getElementById('text').value.split(" ")) myArr.push(x); document.write(myArr); }
14th Mar 2020, 10:52 PM
rodwynnejones
rodwynnejones - avatar
+ 2
Kevin Star Bro but why whenever we input new value it store it in new array ?
14th Mar 2020, 10:55 PM
Abdulaziz Abdurasul
Abdulaziz Abdurasul - avatar
+ 2
EmmanueLZ. YEAHHH BROTHER THATS WORKS))) BUT CAN YOU EXPLAIN WHY IT WORKS LIKE THIS ?🙏🙏
14th Mar 2020, 11:11 PM
Abdulaziz Abdurasul
Abdulaziz Abdurasul - avatar
+ 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)
14th Mar 2020, 11:14 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 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 🤔
14th Mar 2020, 11:16 PM
Abdulaziz Abdurasul
Abdulaziz Abdurasul - avatar
+ 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.
14th Mar 2020, 11:18 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
That's right.
14th Mar 2020, 11:24 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
Indeed the syntax of Kevin Star is better when it come to push your text.value
14th Mar 2020, 11:38 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 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
16th Mar 2020, 8:56 AM
Darcy Dev
Darcy Dev - avatar
+ 1
How to add website in backend
16th Mar 2020, 11:19 AM
Bobby