Record Collection push与赋值语句的区别


#1

Tell us what’s happening:
想问collection[id][prop]=value;与collection[id][prop].push(value);的区别是什么呢?
有点分不清
Your code so far


// 初始化变量
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// 复制 collection 以便测试
var collectionCopy = JSON.parse(JSON.stringify(collection));

// 请把你的代码写在这条注释以下
function updateRecords(id, prop, value) {
  if((prop === "tracks") && (value !== "")){
    if(collection[id][prop]){ //prop是 "tracks"且有"tracks"属性
      collection[id][prop].push(value);  //把value添加到tracks数组中
    }
    else{   //prop是 "tracks"但是专辑中没有 "tracks"属性
      collection[id][prop]=[value];  //给"tracks"创建一个空数组
    }
  }
  else if(value!== ""){  //value不为空
    collection[id][prop]=value;
  }
  else if(value==""){
    delete collection[id][prop];
  }
  return collection;
}

// 你可以修改这一行来测试你的代码
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3732.400 QQBrowser/10.5.3819.400.

Link to the challenge:
https://learn.freecodecamp.one/javascript-algorithms-and-data-structures/basic-javascript/record-collection


#2

前者是赋值,后者是添加