1

Using NodeJs + Express to create a REST API. Everything works well, but I can't understand how to iterate through the request.body and check its fields for undefined and empty values and assign new object only with valid data.

request.body looks like:

{ 
  key: 'value',  
  otherKey: 'otherValue',
  oneMoreKey: '',
  oneMoreKey2: undefined,
  oneMoreKey3: null
}

At that end my object shoud look like:

let contactData = Object.assign({},{
        'key': 'value',
        'otherKey': 'otherValue'
    })

Looking for your advices and help

George
  • 6,630
  • 2
  • 29
  • 36
vladys.bo
  • 730
  • 2
  • 11
  • 34
  • Possible duplicate of [Iterate through object properties](http://stackoverflow.com/questions/8312459/iterate-through-object-properties) – Jozef Cipa May 15 '17 at 14:51
  • Why not use a [bodyParser](https://github.com/expressjs/body-parser) like everyone else, and send valid values ? – adeneo May 15 '17 at 14:51
  • @adeneo I didn't understand how. Using `app.use( bodyParser.urlencoded( { extended: false } ) ); app.use( bodyParser.json() );` settings for `bodyParser` – vladys.bo May 15 '17 at 14:53

2 Answers2

4

JavaScript

function getCleanObject(oldObject) {
    var newObject = {};
    for (var property in oldObject) {
        var value = oldObject[property];
        if (value) newObject[property] = value;
    }
}

Explanation

You can start off by creating a new clean Object

var newObject = {}; // same as new Object();

Then iterate through all of the object's properties using a for loop.

for (var property in oldObject)

Then get the value of that property

var value = oldObject[property];

If the value is Troothy add the property to the new Object

 if (value) newObject[property] = value;

Note that this way the false value will be rejected. To allow it to be copied to the new Object you should replace the if statement with

if(value || value === false)

Moreover, if the Object you are copying also inherits from some other Object it is possible that it will have extra properties as well and if you do not want them to be included you should change the if statement to

if(value && oldObject.hasOwnProperty(value))

And Remember for(var item in object) != for(var item of list)

in is used to iterate through an object's properties whereas of is used to iterate through an iteratable (i.e. list). Also in is supported in all browsers whereas of is not supported by internet explorer.

nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • @vladja Also this will not copy any `false` `boolean` values, if you want `false` `values` to be passed to the new `Object` then change the `if` statement to `if(value || value === false) ` – nick zoum May 15 '17 at 15:09
  • Be careful with `for...in...` loops. This will iterate over all of the object's direct properties and inherited properties from the prototype chain, adding a lot of overhead. You should use `.hasOwnProperty` to ensure you're only inspecting the properties attached to the object itself. – Ian May 15 '17 at 15:11
  • @Shaka This question was about simple Objects so I didn't that was relevant. Do you think I should update the answer? – nick zoum May 15 '17 at 15:13
  • Yeah it wouldn't hurt, I suppose. It's best practice. – Ian May 15 '17 at 15:17
  • @Shaka Okay I fixed it – nick zoum May 15 '17 at 15:30
0
your_object = {
       key: request.body[key] || 'default',
       otherKey: request.body[otherKey] || 'default',
       oneMoreKey: request.body[oneMoreKey] || 'default'
       ...
}

explanation on how or (||) works JavaScript OR (||) variable assignment explanation

Community
  • 1
  • 1
DivineCoder
  • 702
  • 5
  • 11