Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

accessing nested objects in javascript

const user = {
    id: 101,
    email: 'jack@dev.com',
    personalInfo: {
        name: 'Jack',
        address: {
            line1: 'westwish st',
            line2: 'washmasher',
            city: 'wallas',
            state: 'WX'
        }
    }
}

const name = user.personalInfo.name;
const userCity = user.personalInfo.address.city;
Comment

JavaScript Nested Objects

// nested object
const student = { 
    name: 'John', 
    age: 20,
    marks: {
        science: 70,
        math: 75
    }
}

// accessing property of student object
console.log(student.marks); // {science: 70, math: 75}

// accessing property of marks object
console.log(student.marks.science); // 70
Comment

js create nested object from fields

function assign(obj, keyPath, value) {
   lastKeyIndex = keyPath.length-1;
   for (var i = 0; i < lastKeyIndex; ++ i) {
     key = keyPath[i];
     if (!(key in obj)){
       obj[key] = {}
     }
     obj = obj[key];
   }
   obj[keyPath[lastKeyIndex]] = value;
}
Comment

javascript nested objects

Do my eyes decieve me, or is the second answer the same as the first, but
with more upvotes?
Comment

js create nested object from fields

var settings = {};
assign(settings, ['Modules', 'Video', 'Plugin'], 'JWPlayer');
Comment

accessing nested objects in javascript


{key: value, key: value, ...}

Comment

nested object in javascript

const employeeInfo = {
    employeeName: "John Doe",
    employeeId: 27,
    salary: {
        2018-19: "400000INR",
        2019-20: "500000INR",
        2020-21: "650000INR"
    },
    address: {
        locality: {
            address1: "1600 pebble road",
            address2: "Nearby XYZ Bank",
        },
        city: "Mumbai",
        state: "Maharashtra",
        country: "India"
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript mover 
Javascript :: events in javascript 
Javascript :: javascript math.ceil 
Javascript :: jquery remove all alerts 
Javascript :: how to prevent clickjacking in react js 
Javascript :: constructor js 
Javascript :: barcode scanner react js 
Javascript :: react-timeago npm 
Javascript :: react native skeleton 
Javascript :: add class to new element javascript 
Javascript :: how to use if else statement in javascript 
Javascript :: javascript try...catch...finally 
Javascript :: loop into array javascript 
Javascript :: js queryselector 
Javascript :: Why do you need JSON 
Javascript :: Number of trailing zeros of N! 
Javascript :: make custom draggable in react 
Javascript :: fibonacci sequence javascript 
Javascript :: what is weakmap and weakset in javascript 
Javascript :: upload file angular rest api 
Javascript :: search query API example using react 
Javascript :: validatorjs number 
Javascript :: window parent frame 
Javascript :: how to debug node js file in webpack 
Javascript :: validation input javascript 
Javascript :: onchange vue 
Javascript :: react props change 
Javascript :: value js 
Javascript :: javascript if equal infinity 
Javascript :: javascript Program to check if a given year is leap year 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =