Basic JavaScript - Record Collection

I was trying to get a not in python flavor. I tried checking if ‘tracks’ is in object key array of the selected object but it didn’t pass the testing with this code;

// create tracks key
  if ((prop === 'tracks') && ('tracks' in Object.keys(records[id]) === false)) {
    records[id][prop] = []; // create an array
    records[id][prop].push(value);
  }, 
// create tracks key
  if ((prop === 'tracks') && !('tracks' in Object.keys(records[id]))) {
    records[id][prop] = []; // create an array
    records[id][prop].push(value);
  }
 until I used this;
// create tracks key
  if ((prop === 'tracks') && ('tracks' in Object.keys(records[id] === false))) {
    records[id][prop] = []; // create an array
    records[id][prop].push(value);
  }

Please, what’s the difference and why didn’t it pass the test?

Your code so far

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

// Only change code below this line
function updateRecords(records, id, prop, value) {
  // !track and value !empty
  if (prop !== "tracks" && value !== "") {
    records[id][prop] = value
    console.log("// !track and value !empty")
    console.log(records[id][prop])
  }
  // create tracks key
  if ((prop === 'tracks') && ('tracks' in records[id] === false)) {
    console.log("no tracks ***")
    console.log(Object.keys(records[id]))
    console.log(records[id][prop])
    records[id][prop] = []; // create an array
    records[id][prop].push(value);
  }
  // add non-empty string to tracks
  else if (prop === "tracks" && value !== "") {
    console.log("non-empty string")
    records[id][prop].push(value);
    console.log(records[id][prop])
  }
  if (value === "") {
    delete records[id][prop];
  }
  // return entire record collection
  return records;
}
console.log()
updateRecords(recordCollection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

As I understand it, in will look at the indexes of the array as they are the keys of an array object.

const test = [1, 2, 3, 'test'];
console.log('0' in test); // true
console.log('test' in test); // false

const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name'],
  }
};

const keys = Object.keys(recordCollection['2548']);
console.log(keys); // [ 'albumTitle', 'artist', 'tracks' ]
console.log('tracks' in keys); // false
console.log('0' in keys); // true
1 Like

This shows that I can view javascript array as object with indexes as it default keys/properties.
Arigato!