6

Array object:

 var jsonList= {
        "list": [{
            "COLUMN_NAME": "control_master_id",
            "REFERENCED_COLUMN_NAME": "control_master_id",
            "REFERENCED_TABLE_NAME": "tbi_controls_master",
            "TABLE_NAME": "tbi_widget_controls"
        }, {
            "COLUMN_NAME": "authorization_id",
            "REFERENCED_COLUMN_NAME": "authorization_id",
            "REFERENCED_TABLE_NAME": "tbi_authorization_master",
            "TABLE_NAME": "tbi_controls_master"
        }, {
            "COLUMN_NAME": undefined,
            "REFERENCED_COLUMN_NAME": undefined,
            "REFERENCED_TABLE_NAME": undefined,
            "TABLE_NAME": "tbi_widget_controls "
        }]
    }

Expected solution:

var jsonList={
    "list": [{
        "COLUMN_NAME": "control_master_id",
        "REFERENCED_COLUMN_NAME": "control_master_id",
        "REFERENCED_TABLE_NAME": "tbi_controls_master",
        "TABLE_NAME": "tbi_widget_controls"
    }, {
        "COLUMN_NAME": "authorization_id",
        "REFERENCED_COLUMN_NAME": "authorization_id",
        "REFERENCED_TABLE_NAME": "tbi_authorization_master",
        "TABLE_NAME": "tbi_controls_master"
    }, {
        "COLUMN_NAME": "",
        "REFERENCED_COLUMN_NAME": "",
        "REFERENCED_TABLE_NAME": "",
        "TABLE_NAME": "tbi_widget_controls "
    }]
}

Is there any solution to do this using underscore.js?Any ideas? Elegant solutions?

2
  • 2
    Sounds like an X/Y problem. What is producing this? Whatever produced it needs to be fixed Commented Jan 16, 2016 at 7:32
  • Which fuction does create your array? Commented Jan 16, 2016 at 7:45

3 Answers 3

9

You can use this

var updatedList = JSON.stringify(jsonList.list, function (key, value) {return (value === undefined) ? "" : value});

Demo Link Here

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

1 Comment

Woth noting that JSON.stringify returns a string and not an object. You would need to call JSON.parse to convert it back to an object.
4

Apologies for the delay, if you don't care about modifying the existing array and its values then this will probably be a lot quicker performance wise. If you can do anything natively using vanilla js then this should always be used over any libraries in my opinion.

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

You can see the latest version on jsbin here https://jsbin.com/xinuyi/3/edit?html,js,output

Comments

2

This is a very quick solution, I haven't benchmarked it but see the solution below or on jsbin: https://jsbin.com/xinuyi/2/edit?html,js,output

var jsonList = {
    "list": [{
        "COLUMN_NAME": "control_master_id",
        "REFERENCED_COLUMN_NAME": "control_master_id",
        "REFERENCED_TABLE_NAME": "tbi_controls_master",
        "TABLE_NAME": "tbi_widget_controls"
    }, {
        "COLUMN_NAME": "authorization_id",
        "REFERENCED_COLUMN_NAME": "authorization_id",
        "REFERENCED_TABLE_NAME": "tbi_authorization_master",
        "TABLE_NAME": "tbi_controls_master"
    }, {
        "COLUMN_NAME": undefined,
        "REFERENCED_COLUMN_NAME": undefined,
        "REFERENCED_TABLE_NAME": undefined,
        "TABLE_NAME": "tbi_widget_controls "
   }]
};

var updatedList = _.map(jsonList.list, function(object, index) {
    return _.mapObject(object, function(val, key) {
        return (val === undefined) ? "" : val;
    });  
});

3 Comments

No worries, if you don't care about editing the existing array you could use javascript's native each method which would be faster.
TypeError: _.mapObject is not a function.. this error im getting after implmenting this logic in my project.
@SVK the function_.mapObject was added in version 1.8.0. You may be using an older version.

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.