_.identity = function (val) {
return val
};
_.take = function (arr, n) {
if (n === undefined || n < 0) {
return []
}
return _.slice(arr, 0, n)
};
_.drop = function (arr, n) {
if (n === undefined || n < 0) {
return [...arr]
}
return _.slice(arr, n, arr.length)
};
_.last = function (arr, n) {
if (n === undefined || n < 0) {
return _.drop(arr, arr.length-1)
}
return _.drop(arr, arr.length-n)
};
_.each = function (collection, iteratee) {
if (Array.isArray(collection)) {
for (let i = 0; i<collection.length; i++) {
iteratee(collection[i], i, collection)
}
}
else if (typeof collection === 'object') {
for (let key in collection) {
iteratee(collection[key], key, collection)
}
}
};
_.filter = function (arr, test) {
let result = []
_.each(arr, function(el) {
if (test(el)) {
result.push(el)
}
})
return result
};
_.reject = function (arr, test) {
let result = []
_.filter (arr, function(el) {
if (test(el) === false) {
result.push(el)
}
})
return result
};
_.uniq = function (arr) {
let result = []
_.each (arr, function(el) {
if (_.indexOf(result, el) === -1) {
result.push(el)
}
})
return result
};
_.map = function (arr, iteratee) {
let result = []
_.each (arr, function(el) {
result.push(iteratee(el))
})
return result
};
_.pluck = function (arr, keyOrIdx) {
return _.map (arr, function(el) {
return el[keyOrIdx]
})
};
_.reduce = function (arr, iteratee, initVal) {
let start = initVal
_.each(arr, function(el, idx) {
if (start === undefined) {
start = el
}
else {
start = iteratee(start, el, idx, arr)
}
})
return start
};