1. 程式人生 > >es6的find和filter

es6的find和filter

區別在於find返回的是查詢到的第一個物件,filter返回的是查詢到的物件陣列
舉個例子:
list
[{…}, {…}, {…}]0: {name: 1, color: “red”}1: {name: 2, color: “blue”}2: {name: 1}length: 3__proto__: Array(0)

var list2 = list.find(i=>i.name=1)
var list3 = list.filter(i=>i.name
=1)

list2
{name: 1, color: “red”}
list3
(2) [{…}, {…}]0: {name: 1, color: “red”}1: {name: 1}length: 2__proto__: Array(0)