Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript complex literal

// Setup
var 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) {
  if (prop !== 'tracks' && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  } else if (value === "") {
    delete records[id][prop];
  }
  return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Comment

javascript complex literal 2

var 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) {
  if (value === '') {
    delete records[id][prop];
  } else if (prop === 'tracks') {
    records[id][prop] = records[id][prop] || []; // this is called shortcircuit evaluation, see below for explanation
    records[id][prop].push(value);
  } else {
    records[id][prop] = value;
  }
  return records;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript spread operator works on what structure 
Javascript :: on page navigate event javascript 
Javascript :: add decimal places to number javascript 
Javascript :: html video api set speed 
Javascript :: generate html by javascript 
Javascript :: object wrappers in javascript 
Javascript :: get time in between two dates javascript dayjs 
Javascript :: counter plus minus for quantity 
Javascript :: .loads with whole json file 
Javascript :: Plumasil - new item button desc text 
Javascript :: react-folder tree example 
Javascript :: if (arr.indexOf(i) === -1) { return false; 
Javascript :: Nyadorera 
Javascript :: javascript How can i do optional function 
Javascript :: how do i set CORS policy for nodejs sever 
Javascript :: how to change css of menu when scrolling 
Javascript :: angularjs Split date and time from api response 
Javascript :: How do I pass the contents of a textbox into angular js as an input parameter, rather than a $scope variable 
Javascript :: Navigating to another Screen when a button is tapped in React Native 
Javascript :: adding text to ant media stream 
Javascript :: upsert typeorm 
Javascript :: node-mongodb-native keep collection 
Javascript :: aws amplify react site is blank after updating react-router-dom 
Javascript :: Declare JSON Variable In Another File 
Javascript :: ... Notation In JavaScript 
Javascript :: jquery show loader 
Javascript :: javascript check if a number starts with another number 
Javascript :: repate element every 2 seconds 
Javascript :: detect sound chrome extension every 1 second 
Javascript :: react js css style border 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =