Creating and accessing data of JS object's in LWC

What is an Object in JavaScript/LWC:

An object is a list of name-value pairs of primitive data types. Each item in the list is called a property and each property name should be unique.

Here we are not going to explain more about the object. if your interested you can search in google to find more about javascript objects. Here we are going to show, how to create and access data of the Object in Lightning web components.

Creating Objects in LWC:

Almost same how we create object in apex. First we need to define an object and set properties values.
[Account acc = new account();
acc.Name = 'Nagarjuna';
acc.Phone = '1234567890';
System.debug('Name: '+acc.Name);]
But in Javscript we can simply create an object defining properties inside of curly braces.
[import { LightningElement } from 'lwc';
let account = {
            Name: 'Nagarjuna',
            Industry: 'crm',
            Phone: 1234567890
    };
    console.log(account.Name); // Nagarjuna
    
export default class App extends LightningElement {
    
} ]

Accessing Properties in Object LWC:

Like we do access in Apex, using Object.Property name, same way we can also access in lwc.
[Account acc = [Select Id, Name, Phone from Account Limit 1];
System.debug('Name'+acc.Name); // Nagarjuna]
We can access properties in an objects in two ways.
1. Dot Notation
[import { LightningElement } from 'lwc';
let accountDetails = {
        isSelected: true,
        Account: {
            Name: 'Nagarjuna',
            Industry: 'crm',
            Phone: 1234567890
        }
    };
    console.log(accountDetails.isSelected); // true
    console.log(JSON.stringify(accountDetails.Account)); // {"Name":"Nagarjuna","Industry":"crm","Phone":1234567890}
    console.log(accountDetails.Account.Name); // "Nagarjuna"
    
export default class App extends LightningElement {
    
}]
2. Bracket Notation
[import { LightningElement } from 'lwc';
let accountDetails = {
        isSelected: true,
        Account: {
            Name: 'Nagarjuna',
            Industry: 'crm',
            Phone: 1234567890
        }
    };
    console.log(accountDetails['isSelected']); // true
    console.log(JSON.stringify(accountDetails['Account'])); // {"Name":"Nagarjuna","Industry":"crm","Phone":1234567890}
    console.log(accountDetails['Account']['Name']); // "Nagarjuna"
    
export default class App extends LightningElement {
    
}]

COMMENTS

Name

Apex,6,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,JS for LWC,6,Lightning Components,2,lightning Page,1,Lightning Web Components,13,Linux for Windows users,1,LWC,2,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Interview Questions,13,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,Test Class,1,Triggers,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: Creating and accessing data of JS object's in LWC
Creating and accessing data of JS object's in LWC
Creating and accessing data of JS object's in LWC, creating javascript objcts in lwc, accessing object data properties in lightning web components lwc
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://swdcworld.blogspot.com/2020/07/creating-and-accessing-data-of-js.html
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/2020/07/creating-and-accessing-data-of-js.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy