您好,登錄后才能下訂單哦!
JavaScript json 數(shù)組是怎樣的,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
簡(jiǎn)單說,所謂對(duì)象,就是一種無序的數(shù)據(jù)集合,由若干個(gè)“鍵值對(duì)”(key-value)構(gòu)成。
一、數(shù)組作為JSON對(duì)象
[ "Ford", "BMW", "Fiat" ]
在JSON中的數(shù)組,幾乎和在JavaScript中數(shù)組相同。
在JSON中,數(shù)組的值必須是字符串,數(shù)字、對(duì)象、數(shù)組、布爾值或空.
JavaScript中,數(shù)組的值可以是以上所有,再加上其他任何有效的JavaScript表達(dá)式,包括函數(shù)、日期、和undefined。
二、JSON對(duì)象中的數(shù)組
數(shù)組可以是對(duì)象屬性的值:
{ "name":"John", "age":30, "cars":[ "Ford", "BMW", "Fiat" ] }
1. 訪問數(shù)組的值
使用索引號(hào)訪問數(shù)組值:
x = myObj.cars[0];
完整代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>項(xiàng)目</title> </head> <body style="background-color: aqua;"> <p id="demo"></p> <script> myObj = { "name": "John", "age": 30, "cars": ["Ford", "BMW", "Fiat"] } x = myObj.cars[0]; document.getElementById("demo").innerHTML = x; </script> </body> </html>
2. 遍歷數(shù)組
你可以使用for-in循環(huán)遍歷數(shù)組:
for (i in myObj.cars) { x += myObj.cars[i]; }
或者可以使用for循環(huán):
for (i = 0; i < myObj.cars.length; i++) { x += myObj.cars[i]; }
三、JSON對(duì)象中的嵌套數(shù)組
數(shù)組值也可以是另一個(gè)數(shù)組,甚至另一個(gè)JSON對(duì)象:
myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }, { "name":"BMW", "models":[ "320", "X3", "X5" ] }, { "name":"Fiat", "models":[ "500", "Panda" ] } ] }
訪問數(shù)組內(nèi)部的數(shù)組, 使用for-in loop循環(huán):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>項(xiàng)目</title> </head> <body style="background-color: aqua;"> <p>Looping through arrays inside arrays.</p> <p id="demo"></p> <script> var myObj, i, j, x = ""; myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] }, { "name":"BMW", "models":[ "320", "X3", "X5" ] }, { "name":"Fiat", "models":[ "500", "Panda" ] } ] } /*訪問數(shù)組內(nèi)部的數(shù)組, 使用for-in loop循環(huán)*/ for (i in myObj.cars) { x += "<h2>" + myObj.cars[i].name + "</h2>"; for (j in myObj.cars[i].models) { x += myObj.cars[i].models[j] + "<br>"; } } document.getElementById("demo").innerHTML = x; </script> </body> </html>
1. 修改數(shù)組值
使用索引號(hào)修改數(shù)組:
myObj.cars[1] = "Mercedes";
完整代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>項(xiàng)目</title> </head> <body> <p id="demo"></p> <script> var myObj, i, x = ""; myObj = { "name":"John", "age":30, "cars":[ "Ford", "BMW", "Fiat" ] }; myObj.cars[1] = "Mercedes"; for (i in myObj.cars) { x += myObj.cars[i] + "<br>"; } document.getElementById("demo").innerHTML = x; </script> </body> </html>
2. 刪除數(shù)組中的項(xiàng)目
使用delete關(guān)鍵字從數(shù)組中刪除:
delete myObj.cars[1];
關(guān)于JavaScript json 數(shù)組是怎樣的問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。