小程序开发定制JS 数组中的 filter 方法

1、定义

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

2、语法

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

3、参数说明

返回

4、用法

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

5、注意事项

1、filter() 不会对空数组进行检测;

2、filter() 不会改变原始数组。

6、使用实例

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

  1. const array = [14, 17, 18, 32, 33, 16, 40];
  2. const newArr = array.filter(num => num > 14)
  3. console.log(newArr);//打印 [17,18,32,33,16,40]
  4. // 查找某个值-------------------------
  5. const array = [14, 17, 18, 32, 33, 16, 40];
  6. const newArr = array.filter(num => num == 14)
  7. console.log(newArr);//打印 [14]
  8. //返回大于某个值和小于某个值的元素
  9. const array = [14, 17, 18, 32, 33, 16, 40];
  10. const newArr = array.filter(num => num > 14 && num < 33)
  11. console.log(newArr);//打印 [17, 18, 32, 16]

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

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

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

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

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

  1. const array = [
  2. { id: 3 },
  3. { id: 4 },
  4. { id: null },
  5. { id: undefined },
  6. { id: '' },
  7. { id: 0 },
  8. { id: false }
  9. ]
  10. const newArr = array.filter(({ id }) => id)
  11. console.log(newArr);// 打印 [{ "id": 3 },{ "id": 4 }]
  12. //-------------------------------------------------------------------
  13. const array = [undefined, null, 3, 5, 'a', false, 0]
  14. const newArr = array.filter(item => item)
  15. console.log(newArr);// 打印 [3, 5, 'a']

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

  1. const array = [
  2. { name: "a", type: "letter" },
  3. { name: '1', type: "digital" },
  4. { name: 'c', type: "letter" },
  5. { name: '2', type: "digital" },
  6. ];
  7. const newArr = array.filter((item, i, arr) => {
  8. return item.type === "letter"
  9. })
  10. console.log(newArr);
  11. // 打印 [{ "name": "a", "type": "letter" }, { "name": "c", "type":"letter" }]

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

find() 方法返回中满足提供的的第一个元素的值。这里有四个元素,那么就会返回两个数组元素相等的值,这里取反就返回不相等的值, 不取反的时候因为30的元素不符合,所以不返回30的值。

  1. const array = [32, 4, 11, 55, 46, 99, 104, 54, 16, 33, 78, 43, 40]
  2. const oldArr = [32, 33, 16, 40, 30]
  3. function myfunction() {
  4. const result = array.filter(item1 => {
  5. //此处取反去掉,将变换元素状态
  6. return !oldArr.find(item2 => {
  7. return item1 === item2
  8. })
  9. })
  10. return result
  11. }
  12. const newArr = myfunction()
  13. console.log(newArr);
  14. // 取反打印 [4, 11, 55, 46, 99, 104, 54, 78, 43]
  15. // 不取反打印 [32, 16, 33, 40] 此处30的元素不符合,所以不返回30的值

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发