Skip to content

Instantly share code, notes, and snippets.

@JoaoCnh
Created February 14, 2018 10:03
Show Gist options
  • Save JoaoCnh/6bf50fd47709d8da3b64120a853ba0e0 to your computer and use it in GitHub Desktop.
Save JoaoCnh/6bf50fd47709d8da3b64120a853ba0e0 to your computer and use it in GitHub Desktop.
javascript map 3rd example
var mapped = songs.map(function (song) {
// using _.omit we remove the artist property
// don't use delete because it mutates the song object
// while _.omit creates a new one
var newSong = _.omit(song, "artist");
return Object.assign(newSong, {
scrobbleCount: 0,
spotifyUrl: "let's just imagine these properties make sense for now",
});
});
// ES6
const mapped = songs.map(song => {
// let's remove the artist property
const { artist, ...rest } = song;
return {
...rest,
scrobbleCount: 0,
spotifyUrl: "let's just imagine these properties make sense for now",
};
});
console.log(mapped);
// const songs = [
// {
// id: 1,
// name: "Curl of the Burl",
// scrobbleCount: 0,
// spotifyUrl: "let's just imagine these properties make sense for now"
// },
// {
// id: 2,
// name: "Oblivion",
// scrobbleCount: 0,
// spotifyUrl: "let's just imagine these properties make sense for now"
// },
// {
// id: 3,
// name: "Flying Whales",
// scrobbleCount: 0,
// spotifyUrl: "let's just imagine these properties make sense for now"
// },
// {
// id: 4,
// name: "L'Enfant Sauvage",
// scrobbleCount: 0,
// spotifyUrl: "let's just imagine these properties make sense for now"
// },
// ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment