Skip to content

Instantly share code, notes, and snippets.

View vishalkukreja's full-sized avatar
👨‍💻
Learning

Vishal Kukreja vishalkukreja

👨‍💻
Learning
View GitHub Profile
var cityNames = ['Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore'];
console.log(...cityNames); // Mumbai Delhi Pune Ahmadabad Indore
var numbers = [22, 11, 98, 43, 87, 56, 45, 01, 24];
// Without spread operator
console.log("Smallest no.:",Math.min(numbers)); // Smallest no.: NaN
// With spread operator
console.log("Smallest no.:",Math.min(...numbers)); // Smallest no.: 1
// copy an array
var empEligibilityDetails = [
{
"name": "Vishal",
"age": 30,
"isManager": false,
"city": "Mumbai"
},
{
"name": "Kunal",
"age": 31,
//splice() demo program
var cityNames = ['Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore'];
console.log("City names:", cityNames);
// Output : City names: [ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
cityNames.splice(1); // if delete count is omitted, from startIndex, all items will be deleted
console.log(cityNames); //[ 'Mumbai' ]
//Add new elements
//slice() example
var cityNames = ['Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore'];
console.log("City names:", cityNames);
// Output : City names: [ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
var newCities = cityNames.slice(); // gets all - shallow copy
console.log(newCities); //[ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
var newTwoCities = cityNames.slice(1, 3); // start from 1st index and before 3rd
// Basic array
var cityNames = ['Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore'];
console.log("City names:", cityNames);
// Output : City names: [ 'Mumbai', 'Delhi', 'Pune', 'Ahmadabad', 'Indore' ]
console.log("City at 2nd index:", cityNames[2]);
//Add an item to an array
cityNames.push('Jaipur');
console.log("City names:", cityNames);
var colorCodeData = [
{color: "red", value: "#f00"},
{color: "green", value: "#0f0"},
{color: "blue", value: "#00f"},
{color: "cyan", value: "#0ff"},
{color: "magenta", value: "#f0f"},
{color: "yellow", value: "#ff0"},
{color: "black", value: "#000"}
]