[removed] filter() for Objects

前端 未结 16 1052
心在旅途
心在旅途 2020-11-22 15:23

ECMAScript 5 has the filter() prototype for Array types, but not Object types, if I understand correctly.

How would I implemen

16条回答
  •  醉话见心
    2020-11-22 15:29

    If you wish to mutate the same object rather than create a new one.

    The following example will delete all 0 or empty values:

    const sev = { a: 1, b: 0, c: 3 };
    const deleteKeysBy = (obj, predicate) =>
      Object.keys(obj)
        .forEach( (key) => {
          if (predicate(obj[key])) {
            delete(obj[key]);
          }
        });
    
    deleteKeysBy(sev, val => !val);
    

提交回复
热议问题