2024年07月28日 建站教程
想把数组中所有name的参数名改成text,包括所有子集里面的name也改成text,代码其实很简单,如下:
var arr = [{
name:'小李家',
age:'12',
sex:'boy'
},{
name:'小名家',
age:'12',
sex:'boy',
children:[{
name:'小李爸',
age:'65',
sex:'boy'
},{
name:'小李妈',
age:'69',
sex:'girl'
}]
}]
console.log(JSON.parse(JSON.stringify(arr).replace(/name/g,"text")));
//输出结果:
(2) [{…}, {…}]
0: {text: '小李家', age: '12', sex: 'boy'}
1:
age: "12"
children: Array(2)
0: {text: '小李爸', age: '65', sex: 'boy'}
1: {text: '小李妈', age: '69', sex: 'girl'}
length: 2
[[Prototype]]: Array(0)
sex: "boy"
text: "小名家"
[[Prototype]]: Object
length: 2
本文链接:http://so.lmcjl.com/news/9355/