javascript - push array value in variable using java script -


i have 1 string, want split , push in variable.
try push array value in variable result
tried.

var region = "rajkot,jamnagar,surat"; var result; var array = region.split(','); (var i=0; i<array.length; i++ )             {                 alert(array[i]);                 result.push(array[i]);             } 

but returning error result.push not function. how push value on variable , trying alert result variable. please solve query.
thanks.

you should initialize result variable

var result = []; 

so final code be:

var region = "rajkot,jamnagar,surat"; var result = []; var array = region.split(','); (var i=0; i<array.length; i++ ){    alert(array[i]);    result.push(array[i]); } 

but split() returns array, for might unnecessary, unless wish business logic elements of array before adding them results.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -