0

I have an object called data some of the elements are undefined, or simply missing.

How can I do a for loop to assign my personal value to these fields?

data.forEach(function(obj) {
  for(var i in obj) { 
    if(obj[i] === undefined) {
      obj[i] = '';
    }
  }
});

This is currently only replacing the undefined, how can I update to pass an empty value for any field if it doesn't exist?

2
  • where do you want it to pull the keys from? Do you have, like, an array of keys you want this object to have? Commented Mar 21, 2017 at 21:14
  • There's no way of telling, from the code you're showing, what keys do not exist. Commented Mar 21, 2017 at 21:22

2 Answers 2

1

You can create an array with all your keys:

var keys = ['foo', 'bar', 'buzz'];

then iterate over it and check for that is key-given property exists in your object

var obj = {
    foo: 'a'
}

for (var i = 0, n = keys.length; i < n; i++) {
    var key = keys[i];

    if (!obj[key]) {
        obj[key] = '';
    }      
}

Cheers

Sign up to request clarification or add additional context in comments.

3 Comments

"for of" is not supported in my javascript version. could you please modify it?
for (var i = 0, n = keys.length; i < n; i++) { console.log(keys[i]); } ;)
so could you please update your answer with this new modification?
1

If you want to complete the "gaps", in the sense that some of your objects will not have properties that others do have, then you can use this ES6 code:

function fillGaps(data) {
    return new Set([].concat(...data.map(Object.keys))) // create set of all keys
        .forEach( key =>
            data.filter( obj => obj[key] === undefined) // get missing/undefined keys
                .forEach( obj => obj[key] = '' ) ); // set them
}

// Example
var data = [{
    a: 1,
    b: 2
}, {
    a: 10,
    b: 30,
    c: 9
}, {
    a: undefined,
    c: 8
}];

fillGaps(data);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES5 equivalent

function fillGaps(data) {
    // create object with all keys
    keys = Object.keys([].concat.apply([], data.map(Object.keys))
                    .reduce (function (acc, key) {
                        acc[key] = 1;
                        return acc;
                    }, {})
    ).forEach(function (key) {
        return data.filter(function (obj) { // get missing/undefined keys
            return obj[key] === undefined;
        }).forEach(function (obj) {  // set them
            obj[key] = '';
        });
    });
}

// Example
var data = [{
    a: 1,
    b: 2
}, {
    a: 10,
    b: 30,
    c: 9
}, {
    a: undefined,
    c: 8
}];

fillGaps(data);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

thanks, although this is working in absolute, by my "data" is an argument of one of my functions, and i get it as in input, so i need to treat it inside my function only.... however I can't then include your function inside it..
What is the problem with that? Either just grab the code from inside my proposed function (new Set( .... ) or put the function in your code, and call it from within your current function. Even if you place my function inside your function, and then call it, it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.