Skip to content

Instantly share code, notes, and snippets.

@nealalan
Last active August 5, 2019 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealalan/d1d2e4c03a6f552ea02aff53def1a466 to your computer and use it in GitHub Desktop.
Save nealalan/d1d2e4c03a6f552ea02aff53def1a466 to your computer and use it in GitHub Desktop.
JavaScript Notes on Arrays

ARRAY SEARCHING FOR OBJECTS

My original questions was: Creating and accessing array of objects in Javascript

ARRAYS

ACCESS

  • .push = adds to end
  • .pop = removes from end
let sequence = [1, 2, 3];
sequence.push(4);
sequence.push(5);
console.log(sequence);
// → [1, 2, 3, 4, 5]
console.log(sequence.pop());
// → 5
console.log(sequence);
// → [1, 2, 3, 4]
  • .sort = if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1"
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
// Use: string.substr(start, length)
const mySlice = (originalString, 
                startIdx = 0, 
                endIdx = originalString.length) => 
    originalString.substr(startIdx, endIdx - startIdx);
  • .slice = takes start and end indices and returns an array that has elements between. The start index is inclusive, the end index exclusive.
console.log([0, 1, 2, 3, 4].slice(2, 4));
// → [2, 3]
console.log([0, 1, 2, 3, 4].slice(2));
// → [2, 3, 4]
console.log("coconuts".slice(4, 7));
// → nut
  • .concat = example using .slice and .concat together
function remove(array, index) {
  return array.slice(0, index)
    .concat(array.slice(index + 1));
}
console.log(remove(["a", "b", "c", "d", "e"], 2));
// → ["a", "b", "d", "e"]
  • .split; .join
let sentence = "Secretarybirds specialize in stomping";
let words = sentence.split(" ");
console.log(words);
// → ["Secretarybirds", "specialize", "in", "stomping"]
console.log(words.join(". "));
// → Secretarybirds. specialize. in. stomping
  • .indexOf; .lastIndexOf
  console.log([1, 2, 3, 2, 1].indexOf(2));
// → 1
console.log([1, 2, 3, 2, 1].lastIndexOf(2));
// → 3
console.log("coconut".indexOf("u"));
// → 5
console.log("one two three".indexOf("ee"));
// → 11
  • .trim = method removes whitespace (spaces, newlines, tabs, and similar characters) from the start and end of a string.
console.log("  okay \n ".trim());
// → okay
  • .padStart
console.log(String(6).padStart(3, "0"));
// → 006
  • .zeroPad = pad numbers with zeros
let cows = 4;
console.log(`${zeroPad(cows, 3)} Cows`);
// → 004 Cows
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
  • add arbitrary values to an object
let day1 = {
  squirrel: false,
  events: ["work", "touched tree", "pizza", "running"]
};
console.log(day1.wolf);
// → undefined
day1.wolf = false;
console.log(day1.wolf);
// → false
  • delete operator = cuts off a tentacle from such an octopus
let anObject = {left: 1, right: 2};
console.log(anObject.left);
// → 1
delete anObject.left;
console.log(anObject.left);
// → undefined
console.log("left" in anObject);
// → false
  • .keys = get keys as an array, without values
console.log(Object.keys({x: 0, y: 0, z: 2}));
// → ["x", "y", "z"]

ARRARS: OBJECT REFERENCE VS OBJECT .VALUE

  • object1 and object2 bindings grasp the same object, which is why changing object1 also changes the value of object2.
  • object3 points to a different object, which initially contains the same properties as object1 but lives a separate life.
  • Comparing different objects will return false, even if they have identical properties.
let object1 = {value: 10};
let object2 = object1;
let object3 = {value: 10};
console.log(object1 == object2);
// → true
console.log(object1 == object3);
// → false
object1.value = 15;
console.log(object2.value);
// → 15
console.log(object3.value);
// → 10
  • accessing the bindings must be done explicitly
const score = {visitors: 0, home: 0};
// This is okay
score.visitors = 1;
// This isn't allowed
score = {visitors: 1, home: 1};

ARRAY OF OBJECTS

  • explicitly declare the whole journal
let journal = [
  {events: ["work", "touched tree", "pizza",
            "running", "television"],
   squirrel: false},
  {events: ["work", "ice cream", "cauliflower",
            "lasagna", "touched tree", "brushed teeth"],
   squirrel: false},
  {events: ["weekend", "cycling", "break", "peanuts",
            "beer"],
   squirrel: true},
  /* and so on */
];
  • declare it and add to it
let journal = [];

function addEntry(events, squirrel) {
  journal.push({events, squirrel});
}
addEntry(["work", "touched tree", "pizza", "running",
          "television"], false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment