How to filter Object using Array.prototype.filter?

后端 未结 6 1658
夕颜
夕颜 2020-12-03 22:52

Given

var arr = [1,2,true,4,{\"abc\":123},6,7,{\"def\":456},9,[10]]

we can filter number items within array arr using N

6条回答
  •  难免孤独
    2020-12-03 23:26

    Without passing a callback function, you can instead pass in a regex by using the RegExp.prototype.test method and binding a regex

    var arr = [1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]]
    
    var res = arr.filter(RegExp.prototype.test.bind(/\[object Object\]/));
    
    console.log(res)

    This would match any string containing [object Object] as well, but it seems highly unlikely that a string would contain those exact words, unless you have made a mistake and included stringified objects in your array.

提交回复
热议问题