JSFiddle - React, Tailwind, and code Playground

by jdanyow

HTML

<span id="output"></span>

JavaScript

var jso = {
    hello: [{
        foo: 'bar'
    }, {
        abc: {
            def: '123'
        }
    }],
    world: [{
        ride: ['or', 'die']
    }]
};

function replaceValue(arrayOrObject, indexOrKey, newValue) {
    arrayOrObject[indexOrKey] = newValue;
}

// partial application from http://ejohn.org/blog/partial-functions-in-javascript/
Function.prototype.curry = function () {
    var fn = this,
        args = Array.prototype.slice.call(arguments);
    return function () {
        return fn.apply(this, args.concat(
        Array.prototype.slice.call(arguments)));
    };
};

function recurse(object, replacer) {
    if (Object.prototype.toString.call(object) === '[object Array]') {
        var l = object.length;
        while (l--) {
            recurse(object[l], replaceValue.curry(object, l));
        }
    } else if (typeof object === 'object') {
        for (var key in object) {
            recurse(object[key], replaceValue.curry(object, key))
        }
    } else {
        replacer('xyz');
    }
}

recurse(jso);
document.getElementById('output').appendChild(document.createTextNode(JSON.stringify(jso)));