检查 empty/null/undefined 的更好方法

A better way for checking on empty/null/undefined

我想知道我是否可以升级我的代码。 我有以下步骤:当找不到标题时,一本书不可用。当找不到一本书的某些部分元数据时,该书仍然可用,但请求的数据应返回为未知。

var notAvailableMSG = process.env.notAvailableMSG;
var unknownMSG = process.env.unknownMSG;
var bookInfo = [];

// if title is not found = book is not available.
if(bookInfo.title == '' || bookInfo.title == undefined ){
  bookInfo.isbn = isbn;
  bookInfo.title = notAvailableMSG;
  bookInfo.author = notAvailableMSG;
  bookInfo.publisher = notAvailableMSG;
  bookInfo.price = notAvailableMSG;
  bookInfo.currency = notAvailableMSG;
  bookInfo.url = notAvailableMSG;
  bookInfo.releasedate = notAvailableMSG;
  bookInfo.sellable = notAvailableMSG;
}
// Check for other empty values
if(bookInfo.author == '' || bookInfo.author == undefined){
  bookInfo.author = unknownMSG;
}
if(bookInfo.publisher == '' || publisher == undefined){
  bookInfo.publisher = unknownMSG;
}
if(bookInfo.price == '' || bookInfo.price == undefined){
  bookInfo.price = unknownMSG;
}
if(bookInfo.releasedate == '' || bookInfo.releasedate == undefined){
  bookInfo.releasedate = unknownMSG;
}

这段代码可以写成更好的方式让这段代码运行更快(使其更易读)吗?

我认为检查值是否为空的最简单方法是检查它是否为假值。它不是最快的代码,但在我看来更容易阅读:

if(!bookInfo.author){
  bookInfo.author = unknownMSG;
}
if(!bookInfo.publisher){
  bookInfo.publisher = unknownMSG;
}
if(!bookInfo.price && bookInfo.price !== 0){
  bookInfo.price = unknownMSG;
}
if(!bookInfo.releasedate){
  bookInfo.releasedate = unknownMSG;
}

您可以检查 this post 以查看 JS 中的值何时为假:

编辑

如H.B。说你可以用一个函数来检查书的每个键。如果要检查对象的每个键,也可以使用 Object.keys

['author', 'publisher', 'price', 'releasedate', /* ... */].forEach(key => {
  if (bookInfo[key] === undefined || bookInfo[key] === null || bookInfo[key] === '') {
    bookInfo[key] = unknownMSG;
  }
});

另一个可能的重构:

const checkUnknown = prop => {
  if(bookInfo[prop] == '' || bookInfo[prop] == undefined){
    bookInfo[prop] = unknownMSG;
  }
}
checkUnknown('author');
checkUnknown('publisher');
checkUnknown('price');
checkUnknown('releasedate');

更花哨,考虑到标题:

const checkUnknown = (prop, customCallback) => {
  if (bookInfo[prop] == '' || bookInfo[prop] == undefined) {
    if (customCallback)
      customCallback();
    else
      bookInfo[prop] = unknownMSG;
  }
}
checkUnknown('author');
checkUnknown('publisher');
checkUnknown('price');
checkUnknown('releasedate');
checkUnknown('title', () => {
  bookInfo.isbn = isbn;
  bookInfo.title = notAvailableMSG;
  bookInfo.author = notAvailableMSG;
  bookInfo.publisher = notAvailableMSG;
  bookInfo.price = notAvailableMSG;
  bookInfo.currency = notAvailableMSG;
  bookInfo.url = notAvailableMSG;
  bookInfo.releasedate = notAvailableMSG;
  bookInfo.sellable = notAvailableMSG;
});

你不能只在一个 if 子句中这样做吗?

var notAvailableMSG = process.env.notAvailableMSG;
var unknownMSG = process.env.unknownMSG;
var bookInfo = {};

// if title is not found = book is not available.
if(bookInfo.title == '' || bookInfo.title == undefined ){
  bookInfo.isbn = isbn;
  bookInfo.title = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.author = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.publisher = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.price = ((!notAvailableMSG && bookInfo.price != 0) ? unknownMSG : notAvailableMSG);
  bookInfo.currency = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.url = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.releasedate = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.sellable = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
}

当我有很长的属性列表时,我更愿意尽可能避免重复代码或重复 ifs。

在这种情况下,我会选择类似的东西:

var properties = [
  'author',
  'publisher',
  'price',
  'currency',
  'url',
  'releasedate',
  'sellable'
];

if(!bookInfo.title) {
  bookInfo.isbn = isbn;
  bookInfo.title = notAvailableMSG;
  properties.map(function(prop) {
    bookInfo[prop] = notAvailableMSG;
  });
}

// Check for other empty values
properties.map(function(prop) {
  if(!bookInfo[prop]) {
    bookInfo[prop] = unknownMSG;
  }
});