【发布时间】:2016-01-16 13:03:37
【问题描述】:
我被难住了。
这是代码:
const clientInfoPromise = buildPromiseMethod
clientInfoPromise.then((clients) => {
console.log('clients ' + JSON.stringify(clients))
console.log(clients.typeOf)
console.log(_.keys(clients))
这是输出:
clients {}
undefined
['undefined']
我希望 _.keys(clients) 返回一个空数组,而不是带有字符串“未定义”的数组。
_.isEmpty(clients) ? [] : _.keys(clients) 不起作用,因为 _.isEmpty 返回 false。
ClientInfoPromise 在这里定义:
static buildPromiseMethod(cids) {
if (condition) {
// fetch data
const data = [data,data1,data2]
return Promise.resolve(_.zipObject(cids, data));
} else {
const clients = _(cids)
.indexBy()
.mapValues(() => {
return undefined; //contains empty data
})
.value();
return Promise.resolve(clients);
}
}
cids 可以是 undefined、[] 或 [1,2,3](数字数组)。
【问题讨论】:
-
看起来您缺少第一个代码块末尾的右花括号和括号。
-
嗯?
_.keys(undefined).length === 0 -
如果这些答案对您有帮助,请接受其中一个。
标签: javascript ecmascript-6 lodash