Skip to content

Instantly share code, notes, and snippets.

@goodbedford
Created April 2, 2016 09:26
Show Gist options
  • Save goodbedford/114f5c2ee83366441fbecba870f59eb9 to your computer and use it in GitHub Desktop.
Save goodbedford/114f5c2ee83366441fbecba870f59eb9 to your computer and use it in GitHub Desktop.
var week = ["sun", "mon", "tue", "wed", "th", "fri", "sat"];
var workWeek = [];
//we want only mon-fri
workWeek = week.slice(1,6);
console.log("work week:", workWeek);
week[1] = "monday";
console.log("week",week);
console.log("work week:", workWeek);
var months = [
{long: "january",
short: "jan"
},
{long: "february",
short: "feb"
},
{long: "march",
short: "mar"
}];
var twoMonths = [];
twoMonths = months.slice(0,2);
console.log("two months:", twoMonths);
months[0].short = "first month";
console.log("short jan in months:", months[0].short);
console.log("short jan in two months:", twoMonths[0].short);
// Be careful with slicing arrays of objects to copy.
// objects are stored as reference in memory not as values.
// string and numbers are stored as values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment