JavaScript 如何通过 JSON 数据来创建数组?
若要從 JSON 資料建立陣列,請使用 JavaScript 中 map() 的概念。假設我們的資料如下 −
const studentDetails =[
{
name : "John"
},
{
name : "David"
},
{
name : "Bob"
}
];以下程式碼可從上述資料建立陣列 −
範例
const studentDetails =[
{
name : "John"
},
{
name : "David"
},
{
name : "Bob"
}
];
const ListOfStudentName =
studentDetails.map(({name:actualValue})=>actualValue);
console.log(ListOfStudentName);若要執行上述程式,您需要使用以下命令 −
node fileName.js.
在此,我的檔案名稱為 demo82.js。
輸出
這將產生以下輸出 −
PS C:\Users\Amit\JavaScript-code> node demo82.js [ 'John', 'David', 'Bob']
广告
数据结构
网络
关系数据库管理系统
操作系统
Java 编程语言
iOS
HTML
CSS
安卓
Python 编程语言
C 编程语言
C++ 编程语言
C#
MongoDB
MySQL
JavaScript
PHP