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