react json字符串转为数组
时间: 2023-09-30 09:04:19 浏览: 323
react-truncate-string:像黄油一样截断字符串
可以使用JSON.parse()方法将JSON字符串转换为JavaScript对象,然后使用JavaScript的数组方法将对象转换为数组。例如:
```javascript
const jsonString = '[{"name": "John", "age": 30}, {"name": "Mary", "age": 25}]';
const jsonArray = JSON.parse(jsonString);
const array = Array.from(jsonArray);
console.log(array); // output: [{name: "John", age: 30}, {name: "Mary", age: 25}]
```
或者使用ES6的展开运算符:
```javascript
const jsonString = '[{"name": "John", "age": 30}, {"name": "Mary", "age": 25}]';
const jsonArray = JSON.parse(jsonString);
const array = [...jsonArray];
console.log(array); // output: [{name: "John", age: 30}, {name: "Mary", age: 25}]
```
阅读全文