{...}" method to convert the characters Just convert the string into an array object.">
Javascript method of converting characters to arrays: first create a js sample file; then pass "let s1 = str.split(',');s.forEach((item, i)=>{. ..}" method to convert the string into an array object.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to convert characters into arrays in javascript?
JS converts strings into array objects
Convert str = "1,2,3", str2 = "ann,lili,john" "
Convert to: arr = [{id: 1,name: 'ann'},{id: 2, name: 'lili'},{id: 3,name: 'john'}];
The code is as follows:
let arr = []; let s1 = str.split(','); s.forEach((item, i)=>{ let obj = {}; obj.id = item; arr.push(obj) }) let s2 = str2.split(',') s2.forEach((item, i)=>{ arr[i].name = item; })
[Recommended learning:javascript advanced tutorial]
The above is the detailed content of How to convert characters to array in javascript. For more information, please follow other related articles on the PHP Chinese website!