简体   繁体   中英

How to MAP an Object Deeply

I want to change somthing in my all values of object let say for example

var a = {a:{c:1},b:2};
desire output :  a = {a:{c:5},b:10} //multiply by 5

I am doing like this

 var m = function n (o){
    return Object.keys(o).map(function(v,i){
        if(o[v] !== null && typeof o[v] === 'object') return n(o[v])
        else return (o[v]*5);
    })
    }

a = m({a:{c:1},b:2})

But getting output of a

[
  [
    5
  ],
  10
]

To generate one object from another, people will frequently use reduce :

 var a = {a: {c: 1}, b: 2}; var m = function n(o) { return Object.keys(o).reduce(function(newObj, key) { var value = o[key]; if (value !== null && typeof value === 'object') { newObj[key] = n(value); } else { newObj[key] = value * 5; } return newObj; }, {}); }; console.log(m(a)); 

We pass in a new object as the "accumulator" and set properties on it, returning it from the reduce callback each time so it passes through the entire process.

This is sometimes called an "abusage" of reduce , since the value of the accumulator never changes (it's always the same object; the object's state changes, but not the object), whereas normally the reduce callback alters the value passing through it.

If you prefer, forEach closing over the new object is the other common way:

 var a = {a: {c: 1}, b: 2}; var m = function n(o) { var newObj = {}; Object.keys(o).forEach(function(key) { var value = o[key]; if (value !== null && typeof value === 'object') { newObj[key] = n(value); } else { newObj[key] = value * 5; } }); return newObj; }; console.log(m(a)); 

You can do it recursively:

var myObj = {a:{c:1},b:2};

function changeChild(obj) {
    var keys = Object.keys(obj);
    keys.forEach(key => {
        if (typeof obj[key] === "object") {
            changeChild(obj[key])
        } else if (typeof obj[key] === "number") {
            obj[key] = obj[key] * 5;
        }
    });
}

changeChild(myObj);

console.log(myObj);

A cleaner way...

var m = function (object) {
    for(var key in object) {
        if(!!object[key] && typeof object[key] === 'object') {
            object[key] = m(object[key]);
        } else {
            object[key] *= 5;
        }
    }
    return object;
};

Array.map function returns Array..this is the reason...and if you know what to change inside the object, why won't you do it directly?

From my point of view you're doing redundant executions for something really simple.

you're creating an anonymous function, using Object.keys to get a new array (after it iterated your object)

you're using Array.map to create another array from this array and inside it creating ( another ) anonymous function to iterate the new array, and on every iteration you're creating another 4 conditional branches..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM