Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript JSON Arrays

JS JSON Arrays

JSON array literals are surrounded by big braces [].

JSON Array Literals

JSON array literals are surrounded by big braces [].

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

In JSON, array values must be of type string, number, object, array, boolean or null.

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

					 
        
          // This is a JSON string
          '["Ford", "BMW", "Fiat"]'

          // Inside the JSON string there is a JSON array literal
          ["Ford", "BMW", "Fiat"]
        
      
JavaScript Arrays

You can create a JavaScript array from a JSON literal by parsing it.

					 
        
          myJSON = '["Ford", "BMW", "Fiat"]';
          myArray = JSON.parse(myJSON);
        
      
Accessing Array Values

You access array values by index.

					 
        
          myArray[0];
        
      
Arrays in Objects

Objects can contain arrays.

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

You access array values by index.

					 
        
          myObj.cars[0];
        
      
Looping Through an Array

You can access array values by using a for in loop.

					 
        
          for (let i in myObj.cars) {
            x += myObj.cars[i];
          }
        
      

Or you can use a for loop.

					 
        
          for (let i = 0; i < myObj.cars.length; i++) {
            x += myObj.cars[i];
          }
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved