{...}" method to convert the characters Just convert the string into an array object."/> {...}" method to convert the characters Just convert the string into an array object.">

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

How to convert characters to array in javascript

藏色散人
藏色散人Original
2021-06-21 11:06:322734browse

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;
})

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

Statement:
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