Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js 数组差集

作者:古枕清风   发布日期:2025-12-29   浏览:43

// 示例代码:计算两个数组的差集

function arrayDifference(arr1, arr2) {
  // 使用 filter 和 includes 方法,筛选出 arr1 中不在 arr2 中的元素
  const difference = arr1.filter(item => !arr2.includes(item));

  return difference;
}

// 示例用法
const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 6];

const result = arrayDifference(array1, array2);
console.log(result); // 输出: [1, 2, 5]

// 解释:
// arrayDifference 函数接收两个数组作为参数,
// 并返回一个新数组,该数组包含只在第一个数组中存在的元素。
// 在这个例子中,1、2 和 5 只存在于 array1 中,因此它们被保留在结果数组中。

上一篇:js array去重

下一篇:js 数组 findindex

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站