相关疑难解决方法(0)

JS - 深层地图功能

Underscore.js有一个非常有用的map功能.

_.map([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]
_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
=> [3, 6, 9]
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个类似的函数,可以迭代嵌套对象或深度映射.经过大量的搜索,我真的找不到这个.我能找到的东西是采摘深层物体,但不能遍历深层物体的每个值.

像这样的东西:

deepMap({
  one: 1,
  two: [
    { foo: 'bar' },
    { foos: ['b', 'a', 'r', 's'] },
  ],
  three: [1, 2, 3]
}, function(val, key) {
  return (String(val).indexOf('b') > -1) ? 'bobcat' : val;
})
Run Code Online (Sandbox Code Playgroud)

怎么会这样做?

样本输出

{
  one: 1,
  two: [
    { foo: …
Run Code Online (Sandbox Code Playgroud)

javascript json dictionary

15
推荐指数
4
解决办法
1万
查看次数

标签 统计

dictionary ×1

javascript ×1

json ×1