function insertItem(item){
if (myArray.indexOf(item) < 0) {
myArray.push(item);
}
}
function deleteItem(item){
var i = myArray.indexOf(item);
if (i !== -1) {
myArray.splice(i, 1);
}
}
function(myArray){
return myArray.filter(function(name,pos,arr){
return arr.indexOf(name) === pos;
});
Takes firt occurrence of property (array must be sorted)
(arr, prop) => {
const seen = new Set();
return arr.filter(item => {
if(item.hasOwnProperty(prop)) {
const k = item[prop];
return seen.has(k) ? false : seen.add(k);
}
return false;
});
};
Takes last occurrence of property (array must be sorted)
(arr, prop) => {
if(arr[0].hasOwnProperty(prop)) {
return [
...new Map(
arr.map(x => [ x[prop], x ])
).values()
];
}
return [];
};
[1,2,3].reverse()
Must use semi-colon
// [1,2,3]
function reverseArray(arr){
for(let i=0; i < arr.length/2; i++){
[arr[i], arr[arr.length - i - 1]] = [arr[arr.length - i - 1], arr[i]];
}
return arr
}
function reverseArray(arr){
const temp = [];
for(let i = 0; i < arr.length; i++){
temp.push(arr[arr.length - i - 1]);
}
return temp;
}
function reverseArrayRR(arr) {
return arr.reduceRight((acc,cur) => acc.concat(cur),[])
}