I want to access array variable outside the loop. but its returning null. below is sample code.
var result = [];
for (var i=0; i < 10; i++) {
result.push[i];
}
4 Answers 4
The syntex of push method is push() not push[].
var result = [];
for (var i=0; i < 10; i++) {
result.push(i);
}
console.log(result);
For more info about push() look How to append something to an array?
1 Comment
var args ={},fileObject = []; for (var i in files) { base64.encode('/opt/zipoutput/' + files[i], function (err, base64String) { convertval = base64String; var dataObj = { "actions": [ { "file_path": files[i] }] }; fileObject.push(dataObj); }); } console.log("fileObject111-----",fileObject);push is a method implemented on array. The basic syntax of invoking or calling a function is to specify parenthesis () after the function name.
The push() method adds one or more elements to the end of an array and returns the new length of the array.
var result = [];
for (var i=0; i < 10; i++) {
result.push(i);
}
console.log(result);
Comments
Please use the below code:
var result = [];
for (var i=0; i < 10; i++) {
result.push(i);
}
Comments
You can do that like this also.
var result = [];
for (var i=0; i < 10; i++) {
result[i]=i;
}
If you want to use push then use like this result.push(i)
()on push. Likeresult.push(i);result.push[i]does not do anything.push[i]return an error BTW? I'm curiousarray.push[2] = 'foo'creates a property2on thepushmethod with valuefoo.