Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the JavaScript json array like?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report