mirror of
https://github.com/thangisme/notes.git
synced 2024-11-01 01:27:31 -04:00
59 lines
958 B
JavaScript
59 lines
958 B
JavaScript
|
/*!
|
||
|
* arr-diff <https://github.com/jonschlinkert/arr-diff>
|
||
|
*
|
||
|
* Copyright (c) 2014 Jon Schlinkert, contributors.
|
||
|
* Licensed under the MIT License
|
||
|
*/
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
var flatten = require('arr-flatten');
|
||
|
var slice = [].slice;
|
||
|
|
||
|
/**
|
||
|
* Return the difference between the first array and
|
||
|
* additional arrays.
|
||
|
*
|
||
|
* ```js
|
||
|
* var diff = require('{%= name %}');
|
||
|
*
|
||
|
* var a = ['a', 'b', 'c', 'd'];
|
||
|
* var b = ['b', 'c'];
|
||
|
*
|
||
|
* console.log(diff(a, b))
|
||
|
* //=> ['a', 'd']
|
||
|
* ```
|
||
|
*
|
||
|
* @param {Array} `a`
|
||
|
* @param {Array} `b`
|
||
|
* @return {Array}
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
function diff(arr, arrays) {
|
||
|
var argsLen = arguments.length;
|
||
|
var len = arr.length, i = -1;
|
||
|
var res = [], arrays;
|
||
|
|
||
|
if (argsLen === 1) {
|
||
|
return arr;
|
||
|
}
|
||
|
|
||
|
if (argsLen > 2) {
|
||
|
arrays = flatten(slice.call(arguments, 1));
|
||
|
}
|
||
|
|
||
|
while (++i < len) {
|
||
|
if (!~arrays.indexOf(arr[i])) {
|
||
|
res.push(arr[i]);
|
||
|
}
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Expose `diff`
|
||
|
*/
|
||
|
|
||
|
module.exports = diff;
|