简体   繁体   中英

For each element in array add string into second array

For each element of the array arr[], i need to add this string to another array. I tried like this:

 var arr = ["TagID3", "TagID4", "TagID5", "TagID6", "TagID7", "TagID8", "TagID9"]; var newarr = []; for (var i = 0; i < arr.length; i++) { newarr.push({ "id": i, "valueAxis": i, "bullet": "round", "type": "smoothedLine", "valueField": i }) } for (var d = 0; d < newarr.length; d++) { document.write(newarr[d]); } 

But it show only [object Object]

Your code looks fine, however you could use JSON.stringify() to write the result to your document that is readable. This would address the [object Object] issue you noted:

 var arr = ["TagID3", "TagID4", "TagID5", "TagID6", "TagID7", "TagID8", "TagID9"]; var newarr = []; for (var i = 0; i < arr.length; i++) { newarr.push({ "id": i, "valueAxis": i, "bullet": "round", "type": "smoothedLine", "valueField": i }) } /* for (var d = 0; d < newarr.length; d++) { // Use JSON.stringify to produce a string from JSON object // that will write to document in readable way var stringValue = JSON.stringify(newarr[d]) document.write(stringValue); } */ // Write item data from in newarr as comma separated, readable string document.write(newarr.map(JSON.stringify).join()); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM