简体   繁体   English

如何处理Javascript空对象?

[英]How to handle Javascript empty object?

I'm stumped. 我很难过。

This is code: 这是代码:

const clientInfoPromise = buildPromiseMethod
clientInfoPromise.then((clients) => {
    console.log('clients ' + JSON.stringify(clients))
    console.log(clients.typeOf)
    console.log(_.keys(clients))

This is output: 这是输出:

clients {}
undefined
['undefined']

I would like _.keys(clients) to return an empty array instead of an array with the string 'undefined'. 我希望_.keys(clients)返回一个空数组而不是一个字符串'undefined'的数组。 _.isEmpty(clients) ? [] : _.keys(clients) _.isEmpty(clients) ? [] : _.keys(clients) doesn't work, because _.isEmpty returns false . _.isEmpty(clients) ? [] : _.keys(clients)不起作用,因为_.isEmpty返回false

ClientInfoPromise is defined here: 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 can be undefined , [] , or [1,2,3] (array of numbers). cids可以是undefined[][1,2,3] (数字数组)。

console.log(clients.typeOf) 执行console.log(clients.typeOf)

To log the type of clients use console.log(typeof clients) . 要记录clients类型,请使用console.log(typeof clients)

console.log(_.keys(clients)) 的console.log(_。键(客户端))
< ['undefined'] <['undefined']

_.key is reporting that clients has a single key, which is named "undefined" . _.key报告clients有一个名为"undefined"密钥。 Most likely, this key-value pair is not being shown by stringify because its value is also undefined, which means it will be skipped by stringify . 最有可能的是, stringify没有显示这个键值对,因为它的值也是未定义的,这意味着它将被stringify跳过。

To verify this, instead of 要验证这一点,而不是

console.log('clients ' + JSON.stringify(clients)) console.log('clients'+ JSON.stringify(clients))

use 使用

console.log('clients', clients)

This will show you all the properties, including ones that stringify would skip because their value is undefined. 这将显示所有属性,包括stringify将跳过的属性,因为它们的值未定义。

In your particular case, the behavior you report is best explained by a bug in buildPromiseMethod , namely that the promise it returns is being resolved to an object with a single key of "undefined` with an unserializable value. For instance, consider the following: 在您的特定情况下,您报告的行为最好通过buildPromiseMethod的错误来解释,即它返回的promise将被解析为具有单个键“undefined”且具有不可序列化值的对象。例如,请考虑以下内容:

> clients = { undefined: undefined };
> console.log('clients ' + JSON.stringify(clients))
< clients {}

> console.log(_.keys(clients))
< ['undefined']

which is exactly what you are getting. 这正是你得到的。

However, it is not obvious how buildPromiseMethod could be returning such a malformed object. 但是, buildPromiseMethod如何返回这样一个格式错误的对象并不明显。 The most likely culprit is zipObject . 最可能的罪魁祸首是zipObject Is that really the exact source code you are running? 这真的是您运行的确切源代码吗? For instance, _.zipObject([cids], data) (specifying cids as an array, when its value is undefined, while data also contains undefined values) could result in the reported behavior: 例如, _.zipObject([cids], data) (将cids指定为数组,当其值未定义,而data也包含未定义的值)可能导致报告的行为:

var data, data1, data2;       // values are undefined
data = [data, data1, data2];
var cids = undefined;
_.zipObject([cids], data);

> { undefined: undefined }

By the way, it's likely that your use of promises is wrong, at least assuming that // fetch data is asynchronous. 顺便说一句,你使用promises可能是错误的,至少假设// fetch data是异步的。 You probably want 你可能想要

static buildPromiseMethod(cids) {
  if (condition) {
    return fetchdata().then(data => 
      _.zipObject(cids, data));
  }

or something similar. 或类似的东西。 This could be connected to your problem, if data1 etc. is coming back from the promise, and you are trying to access it before the promise completes in which case its value will be undefined. 这可能与您的问题有关,如果data1等从promise中返回,并且您在promise完成之前尝试访问它,在这种情况下它的值将是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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