map function for objects (instead of arrays)

前端 未结 30 1973
无人及你
无人及你 2020-11-22 04:23

I have an object:

myObject = { \'a\': 1, \'b\': 2, \'c\': 3 }

I am looking for a native method, similar to Array.prototype.map

30条回答
  •  悲&欢浪女
    2020-11-22 05:01

    A different take on it is to use a custom json stringify function that can also work on deep objects. This might be useful if you intend to post it to the server anyway as json

    const obj = { 'a': 1, 'b': 2, x: {'c': 3 }}
    const json = JSON.stringify(obj, (k, v) => typeof v === 'number' ? v * v : v)
    
    console.log(json)
    console.log('back to json:', JSON.parse(json))

提交回复
热议问题