Lodash教程

Lodash differenceBy method

语法:

_.differenceBy(array, [values], [iteratee=_.identity])
This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument:(value).

参数

array (Array) − The array to inspect. [values] (...Array) − The values to exclude. [iteratee=_.identity] (Function) − The iteratee invoked per element.

输出:

(Array) − Returns the new array of filtered values.

实例:

var _ = require('lodash');
var numbers = [1.7, 2.4, 3.6, 4.2];
var listOfNumbers = '';
listOfNumbers = _.differenceBy(numbers, [1.3, 2.2], Math.floor);
console.log(listOfNumbers);
listOfNumbers = _.differenceBy([{ 'x': 4 }, { 'x': 1 }], [{ 'x': 4 }], 'x');
console.log(listOfNumbers);
Save the above program in tester.js. Run the following command to execute this program.

命令:

\>node tester.js

输出:

[ 3.6, 4.2 ]
[ { x: 1 } ]
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4