简体   繁体   中英

lodash .get replace empty value

so i am using lodash .get to copy some array from my database to create a excel documents by using this

object[key.key] = _.get(item, key.key, '-');

where key is set of array and key.key is set of array column name or field name. it works fine replacing undefined value from database to - but there is also some fields that just have an empty value and i want to catch those fields and also changing it into -

how to do that?

If there won't be any other "falsy" values the shortest way would be:

obj[key.key] = item[key.key] || '-';

// or with lodash
obj[key.key] = _.get(item, key.key, '-') || '-';

This will replace every "falsy" value with a single dash.

If this isn't possible:

const value = item[key.key];
obj[key.key] = (typeof value === 'undefined' || value === '') ? '-' : value;

// or with lodash
const value = _.get(item, key.key, '-');
obj[key.key] = value === '' ? '-' : value;

这应该工作:

_.get(item, key.key, '-') != '' ? _.get(item, key.key, '-') : '-';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM