let json = [{ id: 1, name: '小白' }, { id: 2, name: '小红' }, { id: 3, name: '小绿' }, { id: 4, name: '小蓝' }] function mySearch (list,keyWords){ if (!keyWords) return list return (list || []).filter(i => { // 用 indexOf 取反时 ~ 比 ! 更好点 return i.id === keyWords || ~i.name.indexOf(keyWords) }) } console.log(mySearch(json,'红')) console.log(mySearch(json,4))