Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
lizlongnc / gist:bbee40c794f12a624a4a
Created November 12, 2014 00:56
Filter out duplicates from an array
from https://coderwall.com/p/nilaba/simple-pure-javascript-array-unique-method-with-5-lines-of-code
Array.prototype.unique = function() {
return this.filter(function (value, index, self) {
return self.indexOf(value) === index;
});
}
var arr = ['a', 1, 'a', 2, '1']
arr.unique(); // => ['a', 1, 2, '1']
use switch statement when if else statements accumulate
var expr = 'Oranges';
switch (expr) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
@lizlongnc
lizlongnc / gist:4da2d50c2dc9222b8534
Created October 20, 2014 05:43
JS break in for loop
var age = Number(prompt('What is your age? ', ' '));
var string = '';
for (var counter = 1; counter < age; counter++) {
string += 'Happy Birthday\n';
if (counter % 7 === 0) {
break;
}
}
@lizlongnc
lizlongnc / gist:1739ec4f663508a66a38
Last active August 29, 2015 14:07
JS common string properties and methods
var str = 'my name is liz';
str.length // returns 14
charAt(index) - returns single char of a string at index postion
str.charAt(0); // returns "m"
str.charAt(3); // returns "n"
indexOf(string)
str.indexOf("n"); // returns 3
@lizlongnc
lizlongnc / gist:90f5d7a263aca2adfffd
Created October 20, 2014 05:02
JS .push() & .pop()
function range(max) {
var retVal = [];
for (var i = 0; i < max; i++) {
retVal.push(i * 2);
}
return retVal;
}
var myArray = range(5);
@lizlongnc
lizlongnc / gist:539e788e6638093da11b
Last active August 29, 2015 14:07
JS FileReader & input type="file"
The input element with a type attribute whose value is "file" represents a list of file items, each consisting of a file name, a file type, and a file body (the contents of the file).
http://www.w3.org/TR/html-markup/input.file.html
http://msdn.microsoft.com/en-us/library/windows/apps/hh466145.aspx
Read Text Files Using the JavaScript FileReader
http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html
READONLY information for an individual file such as its name, size, mimetype, and a reference to the file handle. Adding the "multiple" attribute to the file input element (as in <input type="file" multiple/> will get you a list of files
<input type="file"/>
input always returns a FileList object
@lizlongnc
lizlongnc / gist:15fea0befc3895794a37
Last active August 29, 2015 14:07
JS for(in) - use with JS objects
for(in) loops through the enumerable properties of an object, not the indexes of an array
console.log('The primary use case for for(in) is to enumerate the properties of an object and will result in unexpected results if used with an Array');
console.log('for(in) gives you the index positions of the items if used in the array and not an objecct:');
console.log('the results below are from using a for(in) with var myNewArray = ["a", "b", "c", "d"]');
var myNewArray = ["a", "b", "c", "d"];
for (var i in myNewArray) {
console.log('i', i);
from: http://stackoverflow.com/questions/9329446/how-to-do-for-each-over-an-array-in-javascript
For Actual Arrays
(See "For Array-Like Objects" below for array-like objects.)
You currently have three options and will soon have two more:
Use forEach and related (ES5+)
Use a simple for loop
@lizlongnc
lizlongnc / gist:a802d573e28d7250360f
Last active August 29, 2015 14:07
JS forEach() - arr.forEach(callback[, thisArg])
// arr.forEach(callback[, thisArg])
// REMEMBER!! The forEach uses a function
// The forEach() method executes a provided function once per array element.
// The function can be written outside of the forEach() and called in the forEach() or
// the function can be written inside of the forEach.
// You cannot break from a forEach().
// Each of the 3 code blocks below uses the same function written inside of the forEach().
// also these functions were between script tags in the .html file, if in ext file, they would need to be slightly modified using the this keyword
// For performance-critical scenarios (“hot paths,” tight loops, etc.), a classic for loop is better because the Javascript interpreter can better optimize it. This is also necessary if you need to break from the loop, which you cannot do with forEach.
// http://www.kraigbrockschmidt.com/2013/01/22/array-enumeration-javascript/