简体   繁体   中英

Record collection in javascript

You are given a JSON object representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.

You start with an updateRecords function that takes an object like collection, an id, a prop (like artist or tracks), and a value. Complete the function using the rules below to modify the object passed to the function.

1.Your function must always return the entire object. 2. If prop isn't tracks and value isn't an empty string, update or set that album's prop to value. 3. If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it. 4. If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array. 5. If value is an empty string, delete the given prop property from the album.

Note: A copy of the collection object is used for the tests.

 var collection = { 2548: { albumTitle: 'Slippery When Wet', artist: 'Bon Jovi', tracks: ['Let It Rock', 'You Give Love a Bad Name'] }, 2468: { albumTitle: '1999', artist: 'Prince', tracks: ['1999', 'Little Red Corvette'] }, 1245: { artist: 'Robert Palmer', tracks: [] }, 5439: { albumTitle: 'ABBA Gold' } }; function updateRecords(object, id, prop, value) { if(prop !== 'tracks' && value !== "") { collection[id][prop] = value; } if(prop === 'tracks' && collection[id][prop] === undefined) { collection[id][prop] = [value]; } if(prop === 'tracks') { let track = collection[id][prop]; track.push(value) } if(value === "") { delete collection[id][prop] } console.log(collection) return collection; } updateRecords(collection, 5439, 'artist', 'ABBA'); updateRecords(collection, 2548, 'tracks', ""); updateRecords(collection, 2468, 'artist', "");

what's not working is:-

  1. After updateRecords(collection, 2548, "artist", ""), artist should not be set
  2. After updateRecords(collection, 2548, "tracks", ""), tracks should not be set

Your code is good. But you missed one point here. The object being passed in the updateRecords method is named object . but you are updating an object named collection . You have two option you can either rename the parameter to collection like this,

function updateRecords(collection, id, prop, value) { // changed here.
  
  if(prop !== 'tracks' && value !== "") {
    collection[id][prop] = value;
  }
  if(prop === 'tracks' && collection[id][prop] === undefined) {
    collection[id][prop] = [value];
  }
  if(prop === 'tracks') {
    let track = collection[id][prop];
    track.push(value)
  }
  if(value === "") {
    delete collection[id][prop]
  }
  console.log(collection)
  return collection;
}

Or you can rename all the reference of collection in your updateRecords method to object .

You can try:

function updateRecords(records, id, prop, value) {
  if(prop !== 'tracks' && value !== "") {
    records[id][prop] = value;
  }
  if(prop === 'tracks') {
    if (typeof records[id][prop] === 'undefined') records[id][prop] = [value];
    else records[id][prop].push(value);
  }

  if(value === "") {
    delete records[id][prop]
  }

  return records;
}
// Setup
var collection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },
  1245: {
    artist: 'Robert Palmer',
    tracks: []
  },
  5439: {
    albumTitle: 'ABBA Gold'
  }
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
  if (prop != "tracks" && value != "" ){
     object[id][prop]= value;
   } else if (prop =="tracks" && !(object[id].hasOwnProperty('tracks')))   {
    var tracks=[];
    object[id]["tracks"]= [value];
  }else if (prop=="tracks" && value != ""){
  object[id][prop].push(value);
  } else if (value === ""){
    delete object[id][prop];
  }

  return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

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