📜  Lodash _.drop() 方法

📅  最后修改于: 2022-05-13 01:56:33.414000             🧑  作者: Mango

Lodash _.drop() 方法

Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
Lodash.drop()方法用于删除给定数组中的元素。

句法 :

Lodash.drop(array, number)

参数:

  • 数组:它是要从中删除元素的原始数组。
  • Number:它是要从数组中删除的元素的数量。

注意:元素从数组的索引 0 中删除。

返回值:返回切片数组。

示例 1:

Javascript
// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array = ["a", "b", "c", "d"]
 
// using drop() method to remove
// first two elements
let newArray = _.drop(array, 2)
 
// Printing original array
console.log("before : ", array)
 
// Printing array after applying
// drop function
console.log("after : ", newArray)


Javascript
// Requiring the lodash library
let lodash = require("lodash");
 
// Original array
let array = [1, 2, "a", "b", "c", "d"]
 
// Using drop() method to remove
// first 2 elements from right
let newArray = lodash.dropRight(array, 2)
 
// Printing original array
console.log("before : ", array)
 
// Printing array after applying
// drop function
console.log("after : ", newArray)


Javascript
// Requiring the lodash library
let lodash = require("lodash");
 
// Original array
let array = [1, 2, "a", "b", "c", "d"]
 
// Using drop() method to remove
// first 10 elements
let newArray = lodash.drop(array, 10)
 
// Printing original array
console.log("before : ", array)
 
// Printing array after applying
// drop function
console.log("after : ", newArray)


输出:

示例 2:如果要从数组右侧删除元素,我们使用 _。 dropRight()函数。

Javascript

// Requiring the lodash library
let lodash = require("lodash");
 
// Original array
let array = [1, 2, "a", "b", "c", "d"]
 
// Using drop() method to remove
// first 2 elements from right
let newArray = lodash.dropRight(array, 2)
 
// Printing original array
console.log("before : ", array)
 
// Printing array after applying
// drop function
console.log("after : ", newArray)

输出:

示例 3:如果数字大于给定数组的大小,则返回空数组,如下面给出的示例所示。

Javascript

// Requiring the lodash library
let lodash = require("lodash");
 
// Original array
let array = [1, 2, "a", "b", "c", "d"]
 
// Using drop() method to remove
// first 10 elements
let newArray = lodash.drop(array, 10)
 
// Printing original array
console.log("before : ", array)
 
// Printing array after applying
// drop function
console.log("after : ", newArray)

输出: