ES6是最新版的JavaScript標準,為開發(fā)者提供了便捷的JSON處理方式。下面介紹ES6對JSON的操作。
1.解構(gòu)賦值
let person = {name: 'Tom', age: 18}; let {name, age} = person; console.log(name); //"Tom" console.log(age); //18
2.箭頭函數(shù)
let persons = [ {name: 'Tom', age: 18}, {name: 'Jerry', age: 20}, {name: 'Lucy', age: 22} ]; let names = persons.map(person =>person.name); console.log(names); //["Tom", "Jerry", "Lucy"]
3.模板字符串
let name = 'Tom'; let age = 18; let str = `My name is ${name}, my age is ${age}.`; console.log(str); //"My name is Tom, my age is 18."
4.展開運算符
let person1 = {name: 'Tom'}; let person2 = {age: 18}; let person3 = {...person1, ...person2}; console.log(person3); //{name: 'Tom', age: 18}
以上是ES6對JSON的四種常見操作方式。