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 :: create useTransaction 
Javascript :: add function 
Javascript :: toggling individual item using map in react 
Javascript :: react export multiple components from index 
Javascript :: getting-host-is-not-configured-error-when-using-next-image 
Javascript :: tthree js npm 
Javascript :: Compare a Boolean with another value 
Javascript :: extract image in p5.js 
Javascript :: utm to lat long 
Javascript :: command for importing fetchgraphqlquery in nextjs 
Javascript :: html vue input enabled 
Javascript :: mongoose auto delete after time 
Javascript :: how to add github secrets in javascript 
Javascript :: Google Web App Script Unknown Parameter Error on Load 
Javascript :: vtk js 
Javascript :: Display name instead ID modal dropdown in angularjs 
Javascript :: tabbarbadge style react native 
Javascript :: Porting Promise.all functionality from AngularJs to VueJs 
Javascript :: React Native, <TextInput onChange{(text) = setState(text)} is returning an object instead of a string. Eventhough the default value is a String. Why 
Javascript :: react-native installation error with npx react-native 
Javascript :: access language in request express 
Javascript :: generar numero aleatorio en un rango 
Javascript :: regex online converter 
Javascript :: javascript loop through collection of objects 
Javascript :: Uncaught Error: spawn node 
Javascript :: get longi and long with an adress react 
Javascript :: Javascript Encapsulation Inheritance Polymorphism Composition 
Javascript :: react show more component 
Javascript :: Assigning A Property The Return Value Of A Function In Class 
Javascript :: square brackets javascript object key 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =