Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Nested Data Structures


 1   Array values are accessed by their index in the array in square brackets
 2   Object values are accessed by their property (key) names using dot notation
 
 If you can easily identify an array (enclosed in [square brackets]) and 
 an object (enclosed in {curly braces}), as well as remember the 
 two key points above, you'll find it quite easy to navigate nested 
 data structures. Here is a complex one repesenting a company with their 
 financial statements for example:
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 let company = {
  name: 'Apple, Inc',
  founded: 1976,
  financials: {
    incomeStatement: {
      years: [2020, 2019, 2018],
      revenue: [125, 120, 115],
      costs: [100, 100, 100],
      profit: [25, 20, 15]
    },
    balanceSheet: {
      years: [2020, 2019, 2018],
      assets: [200, 190, 180],
      liabilties: [100, 95, 90],
      equity: [100, 95, 90]
    },
    cashFlow: {
      years: [2020, 2019, 2018],
      operating: [75, 65, 55],
      investing: [22, 20, 18],
      financing: [-94, -80, -75]    
    }
  },
  competitors: ['Microsoft', 'Amazon', 'Samsung']
}

console.log(company.name);
console.log(company.competitors);
console.log(company.competitors[0]);
console.log(company.financials.incomeStatement.years);
console.log(company.financials.incomeStatement.revenue[0]);
 
PREVIOUS NEXT
Tagged: #Nested #Data #Structures
ADD COMMENT
Topic
Name
2+6 =