json去除某属性

剔除age属性

方法一

1
2
3
4
5
6
7
8
9
const arr = {
name: '小王',
id: 1,
age: 18
}
// 剔除age
delete arr.age
console.log(arr)
// 结果:{ id: 1, name: '小王' }

方法二

1
2
3
4
5
6
7
8
9
const arr = {
name: '小王',
id: 1,
age: 18
}
// 剔除age
const {age, ...arr2} = arr
console.log(arr2)
// 结果:{ id:1 , name: '小王' }

方法三

1
2
3
4
5
6
7
8
9
const arr = {
name: '小王',
id: 1,
age: 18
}
// 剔除age
const arr2 = { ...arr2,age:""}
console.log(arr2)
// 结果:{ name: '小王' ,id:1 , age: ""}

参考资料:

https://www.cnblogs.com/chenyablog/p/11713146.html

将字符串转成数组

需求

将以下的数组中的字符串转换成正常的整数数组

1
2
3
4
5
[
"[737, 4534, 35434]",
"[1728, 392, 2713]",
"[0, 0, 3]"
]

实操

思路:JavaScript有一个split函数可以将一段字符串指定某字符切割为字符串数组,然后再将字符串数组转换成整数数组即可。

1
2
3
4
5
6
7
8
9
let nr=obj.data[0].replace("[","").replace("]","").split(",");
let wc=obj.data[1].replace("[","").replace("]","").split(",");
let st=obj.data[2].replace("[","").replace("]","").split(",");
// 1. 将字符串 "[737, 4534, 35434]" 去除前后中括号,得到 ” ‘737’, ‘4534’, ’35434‘ “
// 2. 使用split函数将字符串指定","逗号切割为数组,得到["737", "4534", "35434"]
NumberRead = nr.map(Number)
WordCount = wc.map(Number)
State = st.map(Number)
// 3. 使用 map(Number)将数组中的字符串转换成数字