Home > Web Front-end > JS Tutorial > How to convert characters to array in javascript

How to convert characters to array in javascript

藏色散人
Release: 2021-06-21 11:06:32
Original
2827 people have browsed it

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.

How to convert characters to array in javascript

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;
})
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template