Skip to content

Instantly share code, notes, and snippets.

@patw0929
Last active July 27, 2016 07:12
Show Gist options
  • Save patw0929/058d2013a994389375d1625550718ab5 to your computer and use it in GitHub Desktop.
Save patw0929/058d2013a994389375d1625550718ab5 to your computer and use it in GitHub Desktop.
Array practice
// - slice
const p1Arr = [1, 2, 3, 4, 5, 6];
console.log(p1Arr.slice(0, 3)); // [1, 2, 3]
// - splice
const p2Arr = [1, 2, 3, 4, 5, 6];
p2Arr.splice(1, 3, 9, 9, 9);
console.log(p2Arr); // [1, 9, 9, 9, 5, 6]
// - pop
const p3Arr = [1, 2, 3, 4, 5, 6];
p3Arr.pop();
console.log(p3Arr); // [1, 2, 3, 4, 5]
// - shift
const p4Arr = [1, 2, 3, 4, 5, 6];
p4Arr.shift();
console.log(p4Arr); // [2, 3, 4, 5, 6];
// - unshift
const p5Arr = [1, 2, 3, 4, 5, 6];
p5Arr.unshift(0);
console.log(p5Arr); // [0, 1, 2, 3, 4, 5, 6];
// - every
const p6Arr = [1, 2, 3, 4, 5, 6];
console.log(p6Arr.every(p => p > 3)); // false
// - some
const p7Arr = [1, 2, 3, 4, 5, 6];
console.log(p7Arr.some(p => p > 4)); // true
// - reduce
const p8Arr = [1, 2, 3, 4, 5];
console.log(p8Arr.reduce((curr, prev) => curr + prev)); // 15
// - reduceRight
const p9Arr = [0, 1, 2, 3, 4, 5];
console.log(p9Arr.reduceRight((curr, prev) => curr + prev)); // 15
// - map
const p10Arr = [1, 2, 3, 4, 5];
console.log(p10Arr.map(p => p + 1)); // [2, 3, 4, 5, 6];
// - filter
const p11Arr = [1, 2, 3, 4, 5];
console.log(p11Arr.filter(p => p > 3)); // [4, 5];
// - forEach
const p12Arr = [1, 2, 3, 4, 5];
p12Arr.forEach((v, i) => {
console.log(`${i}: ${v}`);
});
// Notice: Only diff the same number once.
function array_diff(array1, array2) {
var o1 = {};
var o2 = {};
for (var i = 0, len = array1.length; i < len; i++) {
var val = 1;
if (array1[i] in o1) {
val += 1;
}
o1[array1[i]] = val;
}
for (var i = 0, len = array2.length; i < len; i++) {
var val = 1;
if (array2[i] in o2) {
val += 1;
}
o2[array2[i]] = val;
}
// console.log(o1, o2);
var result = [];
for (var i1 in o1) {
for (var i2 in o2) {
if (o1[i1] === 1 && o2[i1] === 1) {
delete o1[i1];
delete o2[i1];
}
}
}
result = [...new Set(Object.keys(o1).concat(Object.keys(o2)))];
return result.map(v => +v).sort(function (a, b) { return a - b; });
}
var array1 = [1, 1, 2, 2, 3, 4, 5, 7, 8, 99];
var array2 = [1, 2, 3, 4, 4, 5, 6, 8, 8, 8];
console.log(array_diff(array1, array2)); // [1, 2, 4, 6, 7, 8, 99]
var crew = [{
age: 10
}, {
age: 22
}, {
age: 5
}, {
age: 30
}];
var sortByAge = function (arr) {
// N^2
// for (var i = 0, maxI = arr.length; i < maxI; i++) {
// for (var j = 0, maxJ = arr.length; j < maxJ; j++) {
// if (+arr[i].age > +arr[j].age) {
// var a = arr[i];
// var b = arr[j];
// arr[i] = b;
// arr[j] = a;
// }
// }
// }
// return arr;
// Quick sort (custom)
if (arr.length <= 1) return arr;
var pivotIndex = Math.floor(arr.length / 2) ;
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++){
if (+arr[i].age < +pivot.age) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return sortByAge(left).concat([pivot], sortByAge(right));
// Sort
// return arr.sort(function (a, b) {
// return +a.age - +b.age;
// });
};
console.log(sortByAge(crew));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment