var objectHTMLCollection = document.getElementsByTagName("select"),
string = [].map.call( objectHTMLCollection, function(node){
return node.id || "";
}).join(" ");
I have this code which converts an [object HTMLCollection]
into a string I am wanting to turn it into an array. So say they output was hello world I am here
would become [hello,world,I,am,here]
You can use split(" ")
var stringArray = string.split(" ");
OR you can prepare array while preparing string, see below code
var stringArray = new Array();
var objectHTMLCollection = document.getElementsByTagName("select"),
string = [].map.call( objectHTMLCollection, function(node){
stringArray.push(node.id);
return node.id || "";
}).join(" ");
Map already returns an array. No need to turn it into a string then back into an array.
var objectHTMLCollection = document.getElementsByTagName("select"), idarray = [].map.call( objectHTMLCollection, function(node){ return node.id || ""; }), string = idarray.join(' '); document.write('<div>String: ' + string + '<div>'); document.write('Array: ' + JSON.stringify(idarray) + '<div>');
<select id='dsada1'></select> <select id='dsada2'></select> <select></select> <select id='dsada4'></select> <select id='dsada5'></select> <select id='dsada6'></select> <br/><br/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.