JavaScriptJavaScript·

JS 数组中的 filter 方法

发布时间:2024-08-29 16:20:14阅读量:36
专业文章
转载请注明来源

1、定义

filter()创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

2、语法

array.filter(function(currentValue,index,arr), thisValue);

3、参数说明

返回

4、用法

filter() 方法用于把Array中的某些元素过滤掉,然后返回剩下的未被过滤掉的元素。

5、注意事项

1、filter() 不会对空数组进行检测;
2、filter() 不会改变原始数组。

6、使用实例

1.返回数组array中所有元素都大于等于14的元素、返回等于14、返回大于某个值和小于某个值的元素的元素。

const array = [14, 17, 18, 32, 33, 16, 40];
const newArr = array.filter(num => num > 14)
console.log(newArr);//打印 [17,18,32,33,16,40]
 
// 查找某个值-------------------------
const array = [14, 17, 18, 32, 33, 16, 40];
const newArr = array.filter(num => num == 14)
console.log(newArr);//打印 [14]
 
//返回大于某个值和小于某个值的元素
const array = [14, 17, 18, 32, 33, 16, 40];
const newArr = array.filter(num => num > 14 && num < 33)
console.log(newArr);//打印 [17, 18, 32, 16]

2.数组去重操作:对数组array中所有相同的元素进行去重复操作。

const array = [2, 2, 'a', 'a', true, true, 15, 17]
const newArr = array.filter((item, i, arr) => {
      return arr.indexOf(item) === i
    })
console.log(newArr);//打印 [2, 'a', true, 15, 17]
 
//-------------------------------------------------------------------------
 
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 9,]
const newArr = array.filter((item, i, arr) => {
      return arr.indexOf(item) === i
    })
console.log(newArr);// 打印 [1, 2, 3, 4, 5, 6, 7, 8, 9]

3、数组中保留奇数或者偶数。

//保留偶数----------------------------------------
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const newArr = array.filter((item, i, arr) => {
      return item % 2 === 0
    })
console.log(newArr);// 打印 [2, 4, 6, 8, 10]
 
//保留奇数----------------------------------------
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const newArr = array.filter((item, i, arr) => {
      return item % 2 !== 0
    })
console.log(newArr);// 打印 [1, 3, 5, 7, 9]

4、去掉数组中的假值,比如:空字符串、undefined、null、0、false。

const array = [
      { id: 3 },
      { id: 4 },
      { id: null },
      { id: undefined },
      { id: '' },
      { id: 0 },
      { id: false }
     ]
const newArr = array.filter(({ id }) => id)
console.log(newArr);// 打印 [{ "id": 3 },{ "id": 4 }]
 
//-------------------------------------------------------------------
 
const array = [undefined, null, 3, 5, 'a', false, 0]
const newArr = array.filter(item => item)
console.log(newArr);// 打印 [3, 5, 'a']

5、把对象数组array中的某个属性值取出来存到数组newArr中。

const array = [
  { name: "a", type: "letter" },
  { name: '1', type: "digital" },
  { name: 'c', type: "letter" },
  { name: '2', type: "digital" },
];
const newArr = array.filter((item, i, arr) => {
  return item.type === "letter"
})
console.log(newArr);
// 打印 [{ "name": "a", "type": "letter" }, { "name": "c", "type":"letter" }]

6、filter结合find方法,实现两个数组的补集的解决方法,oldArr的元素newArr中都有,在newArr中去掉所有的oldArr。

find() 方法返回数组中满足提供的测试函数的第一个元素的值。这里有四个元素,那么就会返回两个数组元素相等的值,这里取反就返回不相等的值, 不取反的时候因为30的元素不符合,所以不返回30的值。
const array = [32, 4, 11, 55, 46, 99, 104, 54, 16, 33, 78, 43, 40]
const oldArr = [32, 33, 16, 40, 30]
function myfunction() {
  const result = array.filter(item1 => {
    //此处取反去掉,将变换元素状态
    return !oldArr.find(item2 => {
      return item1 === item2
    })
  })
  return result
}
const newArr = myfunction()
console.log(newArr);
// 取反打印 [4, 11, 55, 46, 99, 104, 54, 78, 43]
// 不取反打印 [32, 16, 33, 40]   此处30的元素不符合,所以不返回30的值


原文链接:https://blog.csdn.net/a15220216758/article/details/124993673

评论区

暂无评论,来发布第一条评论吧!

弦圈热门内容

Vertically aligning CSS :before and :after content

I am trying to centre the link with the image, but can't seem to move the content vertically in any way.&lt;h4&gt;More Information&lt;/h4&gt; &lt;a href="#" class="pdf"&gt;File Name&lt;/a&gt;The icon is 22 x 22px.pdf { font-size: 12px; } .pdf:before { padding:0 5px 0 0; content: url(../img/icon/pdf_small.png); } .pdf:after { content: " ( .pdf )"; font-size: 10px; } .pdf:hover:after { color: #000; }

数据清洗之异常值处理

在数据清洗过程中,异常值处理是一个非常重要的步骤。异常值,也称为离群值或异常点,是指与大多数观察值明显不同的观察值。这些值可能是由于测量错误、数据录入错误、设备故障或者真实现象的特殊情况而产生的。异常值可能会对数据分析和建模产生负面影响,因此需要进行适当的处理。下面是一些常见的异常值处理方法:删除异常值: 最简单的方法是直接删除异常值。这种方法适用于异常值数量较少、对数据整体影响较小的情况。但是,如果异常值数量较多或者对数据整体影响较大,则不建议使用这种方法。替换异常值: 可以用数据集的其他统计特征值(如均值、中位数、众数)来替换异常值。这种方法可以保持数据集的整体分布特征,并且相对于直接删除异常值,对数据的影响较小。分箱处理: 将数据分成多个箱子(bins),然后用每个箱子的均值或中位数来代替箱子中的异常值。这种方法可以减少异常值对整体数据的影响,并且保持了数据的整体分布特征。基于模型的方法: 可以使用回归、聚类或其他机器学习模型来识别和处理异常值。这些模型可以自动识别数据中的异常值,并提供相应的处理方案。使用离群值检测算法: 可以使用一些专门用于检测异常值的算法,如孤立森林、局部异 ...