Javascript search for an object key in a set

前端 未结 6 831
太阳男子
太阳男子 2021-01-14 09:46

Is it possible to use the javascript \"Set\" object to find an element with a certain key? Something like that:

let myObjects = [{\"name\":\"a\", \"value\":0         


        
6条回答
  •  庸人自扰
    2021-01-14 09:55

    If you want your solution to be performant, use Map instead of Set. If your Set is not that big you can go with T.J. Crowder solution and convert Set to Array then filter and vice versa.

    let myObjects = [['a',{"name":"a", "value":0}], ['b',{"name":"b", "value":1}],['c',{"name":"c", "value":2}]];
    let myMap = new Map(myObjects);
    console.log(myMap.has('a'));
    
    

提交回复
热议问题