1

我正在尝试使用 Underscore.js 单步执行对象并转换/混淆每个值,以便:

{
  "weather": "Cloudy",
  "degrees": 90
}

变成:

{
  "weather": "Claido",
  "degrees": 21
}

完全无法理解的原因 - 我试图在不破坏其结构的情况下混淆 JSON 对象。所以如果它是字符串,我用另一个随机元音替换任何元音。如果它是一个数字,我只是选择另一个随机数字,布尔值是随机设置的,等等。我可以构建每个元素类型的操作,我现在才确定如何递归地进行操作。

我可以做这样的事情:

var t = { "weather":"Cloudy", "degrees": 12, "snowing": false };
var newObj = {};
var g = _.each(t, function(value, key, list) {
  if (_.isString(value)){
     newObj[key] = value.replace(/[aeiou]/gi, '');
  }
  if (_.isNumber(value)){
     var n = Math.floor((Math.random()*10)+1);
     newObj[key] = n + value;
  }
  if (_.isBoolean(value)){
    newObj[key] = Math.random() >= 0.5;
  }
});
console.log(newObj);

一切都很好。问题在于数组和嵌套对象,如下所示:

{
 "weather": "Cloudy",
 "outsideWeather": {
     "forecast": "Stormy",
     "outside": 48
 },
 "degrees": 12,
 "snowing": false
}

那时我的简单 Switch 机箱坏了。任何想法如何递归地遍历每个值,对其进行转换,然后将对象构建回其原始结构?

非常感谢

4

3 回答 3

2
function recurse(mem, val, key) {
  if (_.isObject(val)) {
    mem[key] = _.reduce(val, recurse, {});
  } else {
    mem[key] = obfuscate(val);
  }
  return mem;
}

function obfuscate(value) {
  return "bananas!"; // your obfuscate logic
}

var yourObject = {
  foo: "bar",
  baz: { bizz: "buzz", ok: true }
}

var o = _.reduce(yourObject, recurse, {});
console.log(o);

http://jsfiddle.net/uEwJY/

于 2013-06-25T16:41:16.043 回答
2

我添加了scramble以下划线的功能:http mixin: //jsfiddle.net/ejWCD/2/

var target = {
 "weather": "Cloudy",
 "outsideWeather": {
     "forecast": "Stormy",
     "outside": 48
 },
 "degrees": 12,
 "snowing": false
};

_.mixin({
    scramble: function(obj) {
        var newobj = {};
        _(obj).each(function(el, key) {
            switch( typeof el ) {
                case 'object':
                    newobj[key] = _(el).scramble();
                    break;
                case 'boolean':
                    newobj[key] = !el;
                    break;
                case 'string':
                    newobj[key] = el.replace(/[aeiou]/gi, 'aeiou'.charAt(_.random(4)));
                    break;
                case 'number':
                    newobj[key] = _.random(el*10);
                    break;
            }
        });
        return newobj;
    }
});

console.log( _(target).scramble() );
于 2013-06-25T16:44:46.670 回答
1

我会这样做:

var obfuscate = function(object) {
    if(_.isNumber(object)) {
        return object + 1;
    } else if(_.isString(object)) {
        return object.replace(/[aeiou]/gi, '');
    } else if(_.isBoolean(object)) {
        return Math.random() >= 5;
    } else if(_.isArray(object)) {
        return _.map(object, obfuscate);
    } else if(_.isObject(object)) {
        return _.object(_.map(object, function(value, key) {
            return [key, obfuscate(value)];
        }));
    }
}

注意:我没有混淆key. 如果您想这样做,您可以轻松更改一行代码。另外,如果对象是一个数组,我会混淆每个元素。

测试:

var input = {
 "weather": "Cloudy",
 "outsideWeather": {
     "forecast": "Stormy",
     "outside": 48
 },
 "degrees": 12,
 "snowing": false
};

console.log(obfuscate(input));

http://jsfiddle.net/Dogbert/K2EML/

于 2013-06-25T16:36:03.663 回答