cleartime / --lodash Goto Github PK
View Code? Open in Web Editor NEWlodash,js
lodash,js
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
// console.log(other);
// => [1, 2, 3, [4]]
// console.log(array);
// => [1]
function concat() {
// 判断参数长度
var length = arguments.length;
if (!length) {
return [];
}
var args = Array(length - 1), // 参数
array = arguments[0], // 第一个参数
index = length; // 索引
// 判断长度为0就停止
while (index--) {
// 复制参数导args中
args[index - 1] = arguments[index];
}
// 获取到了所有的参数然后都push到新数组中
return arrayPush(Array.isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1));
}
总结
- 使用while配合index--做循环条件,知道index为0循环停止
- 使用第一种循环配合index-1复制数据
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
function __compact(array) {
let resIndex = 0
const result = []
// 过滤参数为null的情况下返回空数组
if (array == null) {
return result
}
// 迭代器遍历数组
for (const value of array) {
// 如果有数据则放到空数组中
if (value) {
// 索引值对应增加
result[resIndex++] = value
}
}
return result
}
// log(__compact([0, 1, false, 2, '', 3]))
总结
1.判断参数是否为null断定有没有传参
2.遍历数组采用for of
3.返回新数组可以采用增加索引值
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]
// 自己尝试实现个
// 分析思路,参数有2个,用第二个长度来做切割,
function _chunk(arr, num){
let len = arr.length;
if(len<num){
num = len
}
let beishu = Math.ceil(len/num);
let newArr = [];
let temp = 0;
for(let j=0 ; j<beishu ;){
let start = temp;
j++;
let end = j*num;
temp = end;
newArr.push(arr.slice(start,end))
}
return newArr
}
// log(_chunk(['a', 'b', 'c', 'd'], 1))
// 基本算是实现了,但是肯定不是完整,看看源码怎么写的
function chunk(array, size) {
// 转换为数字,防止负数
size = Math.max(size, 0)
// 获取数组的长度
const length = array == null ? 0 : array.length
// 如果数组为空或者切割的长度不够1就返回空数组
if (!length || size < 1) {
return []
}
let index = 0
let resIndex = 0
// 判断切割之后有几个数组,new Array(2)相当于[].length=2
const result = new Array(Math.ceil(length / size))
// 很巧妙的循环判断,边界就是我数组的长度,你从头开始切,切到你需要的尺寸,直到大于数组的长度,就表示不能再切了
// 再一个很巧妙的设计,之前设置了需要切的空数组,每次循环就把当前的数据加入到当前的index里面,循环满了也不用再继续加入了
while (index < length) {
result[resIndex++] = array.slice(index, (index += size))
}
return result
}
总结
- 转换数字且防止负数取最大值
Math.max(size,0)
- 新建空数组
new Array(length)
- 利用空数组配合循环
while(index<length)array[index++]
打造array.push的效果
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.