最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to replace undefined values with empty string in an array object? - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked Jan 16, 2016 at 7:28 SVKSVK 2,1972 gold badges21 silver badges41 bronze badges 2
  • 2 Sounds like an X/Y problem. What is producing this? Whatever produced it needs to be fixed – mplungjan Commented Jan 16, 2016 at 7:32
  • Which fuction does create your array? – user5780762 Commented Jan 16, 2016 at 7:45
Add a ment  | 

3 Answers 3

Reset to default 9

You can use this

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

Demo Link Here

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./xinuyi/3/edit?html,js,output

This is a very quick solution, I haven't benchmarked it but see the solution below or on jsbin: https://jsbin./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;
    });  
});
发布评论

评论列表(0)

  1. 暂无评论