1.获取的json数据是这样的

data:[
  {
    'Id': '1',
    'Phone': '123456',
    'Name': '张三',
  },
  {
    'Id': '2',
    'Phone': '78945',
    'Name': '李四',
  },
  {
    'Id': '3',
    'Phone': '123789',
    'Name': '王五',
  }
]

将数组中的每个对象的Name取出来,产生一个新的数组

let results = data.map(data => n.name);//['张三','李四','王五']

判断数组中是否存在某个值

let results = data.filter(item => item.Name==='王五' );

去掉空数组空字符串、undefined、null

var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter(item => item)

过滤不符合项

var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf('2')<0) 

数组去重

方法1:
var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index)  

方法2:
var arr = [1,2,3,4,3,4]; 
var arr2 = [...new Set(arr)];

交换两个变量的值

let [x,y] = [1,2];
[y,x] = [x,y];

将数组转成对象

var data=[1,2,3,4]
var data1 ={...data}

过滤两个数组对象不同的值

let arr1=[{id:1,name:'网'},{id:2,name:'二位'}]
let arr2=[{id:1,name:'问问'},{id:3,name:'多少'},{id:44,name:'多少'},{id:45,name:'多少'},]

let result = arr2.filter((item) => !arr1.some((ele) => ele.id=== item.id));

//result = [{id:3,name:'多少'},{id:44,name:'多少'},{id:45,name:'多少'}]

墨城
1.7k 声望2.1k 粉丝