Home > Net >  How do i check if key exists in nested object?
How do i check if key exists in nested object?

Time:08-17

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?

CodePudding user response:

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;
}

CodePudding user response:

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"));

  • Related