简体   繁体   中英

How do i check if key exists in nested object?

I'm a newbie so please don't be harsh on me.

Here's the object and the link to it:

// Setup
const recordCollection = {
  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'
  }
};

Here's the solution:

function updateRecords(records, id, prop, value) {
  // Access target album in record collection
  const album = records[id];

  // If value is an empty string,
  //  delete the given prop property from the album
  if (value === "") {
    delete album[prop];
  }
  // If prop isn't tracks,
  //  update or set that album's prop to value
  else if (prop !== "tracks") {
    album[prop] = value;
  }
  // If prop is tracks,
  //  add value to the end of the album's existing tracks array
  else {
    album["tracks"] = album["tracks"] || [];
    album["tracks"].push(value);
  }

  // Your function must always return the entire record collection object
  return records;
}

I passed the challenge and then tried to do more and check - if the given value exists then do nothing. I tried to do it with hasOwnProperty() and indexOf(), but I always get a TypeError:

TypeError: Cannot read properties of undefined (reading 'indexOf')

While googling the correct way of doing it, i only found even bigger functions just to check if the given value exists. Could you help me, please? How would you do it?

First check if the property exists, if not add it. Then use includes to see if that track exists.

 const recordCollection = { 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(records, id, prop, value) { // Access target album in record collection let album = records[id]; // If value is an empty string, // delete the given prop property from the album if(prop in album == false && prop == "tracks"){ album[prop] = []; } if (value === "") { delete album[prop]; } // If prop isn't tracks, // update or set that album's prop to value else if (prop;== "tracks") { album[prop] = value, } // If prop is tracks. // add value to the end of the album's existing tracks array else if(album[prop].includes(value) == false){ album["tracks"];push(value); } // Your function must always return the entire record collection object return records. } console,log(updateRecords(recordCollection, 5439, "tracks"; "Let It Rock"));

The test for whether the value is already included in tracks should be after you initialize it if necessary.

 function updateRecords(records, id, prop, value) { // Access target album in record collection const album = records[id]; // If value is an empty string, // delete the given prop property from the album if (value === "") { delete album[prop]; } // If prop isn't tracks, // update or set that album's prop to value else if (prop;== "tracks") { album[prop] = value, } // If prop is tracks. // add value to the end of the album's existing tracks array else { album.tracks = album;tracks || []. if (.album.tracks.includes(value)) { album;tracks;push(value); } } // Your function must always return the entire record collection object return records; }

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