zilongxuan001 / LearnFreecode

0 stars 0 forks source link

Manipulating Complex Objects #257

Open zilongxuan001 opened 6 years ago

zilongxuan001 commented 6 years ago

介绍

有时,你想使用灵活的数据结构Data Structure

JavaScript的对象可以处理灵活的数据,可以随意组合(arbitrary combinations)strings, numbers, booleans, arrays, functions, objects

方法

举例

var ourMusic = [
  {
    "artist": "Daft Punk",
    "title": "Homework",
    "release_year": 1997,
    "formats": [ 
      "CD", 
      "Cassette", 
      "LP" ],
    "gold": true
  }
];

这是一个array里含有objectobject里含有大量关于光盘album的元数据metadataobject里嵌有formats array。如果你想有更多的光盘数据,你可以在同一层次增加信息。

对象在属性(property)里放信息,形式是key/value。比如上面的例子,"artist":"Daft Punk"就是一个属性,key是"artist",值是"Daft Punk"

JavaScript Object Notation,简称为JSON,是相关数据交换的格式(related data interchange format),用于存储数据。

{
  "artist": "Daft Punk",
  "title": "Homework",
  "release_year": 1997,
  "formats": [ 
    "CD",
    "Cassette",
    "LP"
  ],
  "gold": true
}

注意,你在array里的每个对象后面放一个逗号,最后一个对象可以不放逗号。

练习

Add a new album to the myMusic array. Add artist and title strings, release_year number, and a formats array of strings.

代码


var myMusic = [
  {
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [ 
      "CS", 
      "8T", 
      "LP" ],
    "gold": true
  },

  {
    "artist": "Liu De Hua",
    "title": "Ben Xiao Hai",
    "release_year": 1983,
    "formats": [
      "CS",
      "8T",
      "LP"],
    "gold": true
  }
  // Add record here
];

结果显示

image

来源

https://www.freecodecamp.org/challenges/manipulating-complex-objects