Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the JavaScript json array like?

Shulou Source: shulou.com Published: 2022-06-03 08:30:34 10月18日 Update

What is the JavaScript json array? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Simply put, an object is an unordered collection of data consisting of several "key-value pairs" (key-value).

Array as JSON object

["Ford", "BMW", "Fiat"]

Arrays in JSON are almost the same as arrays in JavaScript.

In JSON, the value of an array must be a string, a number, an object, an array, a Boolean, or empty.

In JavaScript, the values of the array can be all of the above, plus any other valid JavaScript expressions, including functions, dates, and undefined.

2. Array in JSON object

An array can be the value of an object property:

{"name": "John", "age": 30, "cars": ["Ford", "BMW", "Fiat"]}

1. Access the values of the array

Use index numbers to access array values:

X = myObj.cars [0]

Complete code:

Project

MyObj = {"name": "John", "age": 30, "cars": ["Ford", "BMW", "Fiat"]} x = myObj.cars [0]; document.getElementById ("demo") [xss_clean] = x

two。 Ergodic array

You can loop through the array using for-in:

For (I in myObj.cars) {x + = myObj.cars [I];}

Or you can use a for loop:

For (I = 0; I)

< myObj.cars.length; i++) { x += myObj.cars[i]; }

3. Nested arrays in JSON objects

The array value can also be another array or even another JSON object:

MyObj = {"name": "John", "age": 30, "cars": [{"name": "Ford", "models": ["Fiesta", "Focus", "Mustang"]}, {"name": "BMW", "models": ["320", "X3", "X5"]}, {"name": "Fiat" "models": ["500", "Panda"]}]}

Access the array inside the array, using the for-in loop loop:

Project

Looping through arrays inside arrays.

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,500", "Panda"]}} / * access the array inside the array, using the for-in loop loop * / for (I in myObj.cars) {x + = "" + myObj.cars.name + "". For (j in myObj.cars.models) {x + = myObj.cars.models [j] + "

";}} document.getElementById (" demo ") [xss_clean] = x

1. Modify array values

Use index numbers to modify the array:

MyObj.cars [1] = "Mercedes"

Complete code:

Project

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] + "

";} document.getElementById (" demo ") [xss_clean] = x

two。 Delete items in an array

Use the delete keyword to remove from the array:

Delete myObj.cars [1]

This is the answer to the question about what the JavaScript json array is. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Tags: Array object project loop question code more index index number help solution easy valid same simple key keyword content function character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei Microsoft Linux Docker