Giter Club home page Giter Club logo

--lodash's People

Watchers

 avatar  avatar

--lodash's Issues

## 每天一发鲁大师loadsh-Array-chunk(1)

每天一发鲁大师loadsh-Array-chunk

定义: 创建一个元素数组,将元素分成大小的长度。如果数组无法均匀分割,则最终的块将是剩余的元素。

例子:

_.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
}

总结

  1. 转换数字且防止负数取最大值 Math.max(size,0)
  2. 新建空数组 new Array(length)
  3. 利用空数组配合循环 while(index<length)array[index++] 打造array.push的效果

每天一发鲁大师loadsh-Array-concat(2)

定义: 使用任何其他数组和/或值创建一个新数组连接数组

例子:

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));
}

总结

  1. 使用while配合index--做循环条件,知道index为0循环停止
  2. 使用第一种循环配合index-1复制数据

每天一发鲁大师loadsh-Array-compact(3)

定义: 创建一个删除了所有false值的数组。值false,null,0,“”,undefined和NaN。

例子:

_.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.返回新数组可以采用增加索引值

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.