当前位置:网站首页>JS how to convert a string into an array object
JS how to convert a string into an array object
2022-06-12 12:23:00 【Cherry*】
js How to convert a string into an array object
// The data format is as follows :
list: [
{
radio: "",
formInline: "1,2,3,4", // Process this string
},
{
radio: "",
formInline: "1,2,3,4",
},
],
// First cycle list
let str = this.list;
for(var i = 0;i<str.length;i++){
let arr = str[i].formInline.split(",").map((item)=>{
let obj = {
};
obj.name = item;
return obj;
})
this.list[i].formInline = arr; // take arr Assign to array
console.log(this.list[i].formInline);
}
- split() Method is used to put a A string is split into an array of strings . grammar : stringObject.split(separator,howmany)
- separator It's necessary . String or regular expression , Split from where specified by this parameter stringObject.
- howmany Optional . This parameter specifies the maximum length of the returned array . If this parameter is set , No more substrings will be returned than the array specified by this parameter . If the parameter is not set , The entire string will be split , Regardless of its length .
"2:3:4:5".split(":") // Will return ["2", "3", "4", "5"]
"|a|b|c".split("|") // Will return ["", "a", "b", "c"]
map() Method to create a new array , The result is that each element in the array is the return value after a call to the supplied function .
const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// Results output :[2, 8, 18, 32]

The same is true if it is a string
let str = "1,2,3,4";
let arr=str.split(',').map(item=>{
let obj = {
};
obj.name = item;
return obj
})
console.log(arr)
give the result as follows :
边栏推荐
- [译] QUIC Wire Layout Specification - Packet Types and Formats | QUIC协议标准中文翻译(2) 包类型和格式
- 元宇宙是短炒,还是未来趋势?
- MySQL review
- Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
- 导航中,添加边框影响布局的解决方法
- this的指向
- AND THE BIT GOES DOWN: REVISITING THE QUANTIZATION OF NEURAL NETWORKS
- [transfer]placement NEW
- QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘
- Dom+js+ carousel map + no time
猜你喜欢

你不会只会用console.log()吧?

Batch load/store instructions of arm instruction set

安装canvas遇到的问题和运行项目遇到的报错

Must do skill -- use ffmpeg command to quickly and accurately cut video

元宇宙是短炒,还是未来趋势?

Open source project - (erp+ Hotel + e-commerce) background management system

Matlab install license manager error -8

KDD2022 | 边信息增强图Transformer

mysql复习

Dom+js+ carousel map + no time
随机推荐
promise的理解已经利用promise实现图片的预加载(顺序加载)
作物模型的区域模拟实现过程初探
Problems encountered in installing canvas and errors encountered in running the project
For in and object The difference between keys()
JS method of exporting DOM as picture
爱可可AI前沿推介(6.12)
MVC mode, encryption, jsonwebtoken
[translation] go references - the go memory model | memory model for Chinese translation of official golang documents
[转]placement new
Longest string without duplicate characters (leetcode 3)
Rust语言学习
Macro compilation preprocessing header Win32_ LEAN_ AND_ MEAN
Win7 registers out of process components, services, and COM component debugging
JS pre parsing, object, new keyword
QT adds a summary of the problems encountered in the QObject class (you want to use signals and slots) and solves them in person. Error: undefined reference to `vtable for xxxxx (your class name)‘
What is modularity? Benefits of modularity
imx6-uboot添加lvds1显示
Quic wire layout specification - packet types and formats | quic protocol standard Chinese translation (2) package type and format
Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220602 version deployed interface applicable to any development language
Performance comparison test of channel and condition variables of golang in single production and single consumption scenarios