简体   繁体   中英

Why is push shown as undefined, even though it is a keyword and i have not declared this as any object?

function updateRecords(id, prop, value) {
  if (prop !== "tracks" && value !== ""){
    collection[id][prop] = value;
  }


  if (collection[id].hasOwnProperty("tracks") == true){
    if (value === ""){
      delete collection[id].prop;
    }
    else{
      collection[id].tracks.push(value);
    }
  }

  if (prop === "tracks" && collection[id].hasOwnProperty(prop) == false){
    collection.prop = [];
    collection[id].prop.push(value);


  }



  return collection;
}

So the function should add new values to the arrays, but I am not sure why i keep getting an error saying it is push of undefined.

It seems like you are trying to push something to collection[id].prop ?

You must have left out the index reference for collection at the previous line:

collection[id].prop = [];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM