
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to import an Object with Sub Objects and Arrays in JavaScript?
Following is the code for importing an object with Sub objects and arrays in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result{ font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>Importing an Object with Sub Objects and Arrays</h1> <div class="result" style="color: green;"></div> <script> let sampleEle = document.querySelector(".sample"); let obj = JSON.parse(sampleEle.innerHTML); </script> <script src="script.js" type="module"> </script> </body> </html>
script.js
import obj from "./sample.js"; let resultEle = document.querySelector(".result"); for (let i in obj) { resultEle.innerHTML += "Property = " + i + " : Value = " + obj[i] + ""; }
sample.js
export default{ firstName: "Rohan", lastName: "Sharma", school: { name: "St Marks", address: "USA", }, sports: ["cricket", "football"], };
Output
The above code will produce the following output −
- Related Articles
- How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
- Convert array of objects to an object of arrays in JavaScript
- How to merge an array with an object where values are arrays - JavaScript
- How to merge two arrays with objects in one in JavaScript?
- How to transform object of objects to object of array of objects with JavaScript?
- How to represent the source code of an object with JavaScript Arrays?
- How to combine two arrays into an array of objects in JavaScript?
- JavaScript Converting array of objects into object of arrays
- Converting array of objects to an object of objects in JavaScript
- How to remove all blank objects from an Object in JavaScript?
- Converting array of objects to an object in JavaScript
- How to merge objects into a single object array with JavaScript?
- Splitting an object into an array of objects in JavaScript
- Converting array of arrays into an object in JavaScript
- Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?

Advertisements