3

I need to convert values from one Array variable into fields of another variable in Javascript.

My variable is

field = ["entries", "body"]

and it needs to become something like

req.body.entries.body

I tried doing something like

field.forEach(function(prop){
   req.body[prop] = "...";
}

but that works only on req.body.entries and req.body.body. And I need it to go all the way to req.body.entries.body

I'm doing this to get the data from a form in a document field (named entries[body]), do some cleaning up on that data, and then pass it back to node.js as if it was the request that was originally made.

UPDATE

All I need to do is to basically

exports.sanitize = function(field){

    // field = ["entry","body"];

    return function(req, res, next){
        val = getField(req, field); // val = "Some string with data"
        val = some_process(val); // some_process is a function that cleans up the string

        req.body.entry.body = val; // HOW TO TAKE entry.body from the field array???

        next();
    }
};

As you can see, all I want is to NOT hard code entry.body but take it from field array values that passes the name of the field for processing.

If you can think of a more elegant solution to do this, please, let me know.

Thanks!

Aerodynamika
  • 7,883
  • 16
  • 78
  • 137

4 Answers4

1

This works:

var fields = [ "field1", "field2", "field3", "field4" ];
var obj = req.body;
fields.forEach(function(prop) {
    obj[prop] = {};
    obj = obj[prop];
});
  • Thanks @CleanerCoder but I need to actually change the name of the variable itself, not to get its data, change it, and then pass it as another variable. – Aerodynamika Feb 12 '14 at 17:52
  • 1
    @deemeetree can you make your example more clear by not duplicating body in the example? – CleanerCoder Feb 12 '14 at 18:05
  • 1
    ok so basically I have a global req.body variable which contains all the data from the form. i want to make a special function that could work on _any_ field in that variable, such as req.body.entry.text for example or req.body.entry.title, take that field, do some processing on it, and then return it back to that same variable, kind of insert the array value back transformed. shall i rewrite it a bit clearer, or this is understandable? – Aerodynamika Feb 12 '14 at 18:10
  • 1
    Is this what you mean: req.body.entry.title = do_processing(req.body.entry.title) – CleanerCoder Feb 12 '14 at 18:30
  • basically yes, but the problem here is that entry.title is only one of the instances. the same function should also be able to work for req.body.entry.text for example. – Aerodynamika Feb 12 '14 at 18:38
  • I guess you would need to write do_processing(obj) to iterate over the fields of the input obj, process them and set them to the processed value. Does that make sense? – CleanerCoder Feb 12 '14 at 19:19
  • updated my question, so probably your solution could work, but I don't know how to implement it. Thanks! – Aerodynamika Feb 12 '14 at 19:37
  • This is it: `req.body[field[0]].[field[1]] = val;` – CleanerCoder Feb 12 '14 at 19:48
0

You want

var obj = req.body;
for (var i=0; i<fields.length-1; i++)
    obj = obj[fields[i]];
var prop = fields[i];

obj[prop] = cleanUp(obj[prop]);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

This will work only if req.body.entries.body is already defined:

field = ["entries","body"];

var toeval="req.body";
field.forEach(function(prop){
       toeval+="."+prop;
});
toeval+="=\"...\"";
eval(toeval);
ojovirtual
  • 3,332
  • 16
  • 21
  • Don't they say that eval is evil, especially when dealing with user input? – Aerodynamika Feb 12 '14 at 18:43
  • I'm still wondering about eval. As a beginner in JavaScript I'd like to know your opinion :) Thanks! – Aerodynamika Feb 13 '14 at 12:26
  • I'll definitely give it a try. Just think on eval as it will "eval" the string you pass as Javascript. So, if you pass user entry data, someone could pass something "evil" that will execute. Think on it like this: who defines your "field" array? What will you do in your "some_proccess" function? – ojovirtual Feb 13 '14 at 14:00
0

I was able to accomplish this using recursion. Hope this helps!

var foo = {
    bar: {}
};

var fields = ['foobar', 'barfoo'];

function setVar (currentVar, arr, counter) {
    if (arr[counter] === undefined) {
        return;
    }

    currentVar[arr[counter]] = {};
    setVar(currentVar[arr[counter]], arr, counter + 1);
}

setVar(foo.bar, fields, 0);
Walter Roman
  • 4,621
  • 2
  • 32
  • 36