【问题标题】:Typescript push to specific key in array打字稿推送到数组中的特定键
【发布时间】:2017-12-18 23:22:41
【问题描述】:

我正在使用 Firebase 开发 Angular2 项目。

我需要将所有查询结果推送到this.guestPush中的同一个键下。

我有一个包含不同用户级别的多选元素。 4、6、9。

选择一个级别时,生成的对象将保存在guestPush阵列中,但选择另一个级别时会发生以下错误。

我收到此错误:

ERROR TypeError: this.guestPush[("filter" + i)].push 不是函数

这是我的代码:

  guestPush: any[] = new Array;

  level.forEach((lvl, index) => {

    this.db.object(`levelsUsers/${lvl}/users`)
      .subscribe(users => {

        if (this.guestPush['filter_' + i]) {
          this.guestPush['filter_' + i].push(users);
        } else {
          this.guestPush['filter_' + i] = users;
        }

      }, err => console.log(err));

  });

编辑***

包含匹配过滤器的用户的用户对象:

Object {-KmSjAaxdCUvrPEQ8InI: Object, $key: "users", $exists: function}
  -KmSjAaxdCUvrPEQ8InI: Object
    admin:"false"
    description:"desc"
    ...
    ...
    verified:false
  __proto__:Object
  exists:function ()
  $key:"users"
  __proto__:Object

this.guestPush[i] = users; 创建一个像这样的对象:

[Object]
 0:Object
 -KmSjAaxdCUvrPEQ8InI: Object
    admin:"false"
    description:desc"
    ...
    ...
    verified:false
    __proto__:Object
  $exists:function ()
  $key:"users"
  __proto__:Object
 length:1
 __proto__:Array(0)

因此,在下一个循环中,我想在 -KmSjAaxdCUvrPEQ8InI 旁边添加任何新的用户对象,或者只要将其添加为 0 键的值即可。

【问题讨论】:

  • 您有“this.db.object”这一行,也许将其更改为列表订阅会有所帮助?如果您使用的是 angularfire,那将返回一个对象,因此没有推送方法。 Reference
  • 因为这里的一切都是箭头函数,我有点迷失了thisguestPush 是变量还是属性?
  • 这是一个属性。
  • Typescipt 支持来自 JS 的未来功能。其中之一是地图。您可以重写代码而不将数组视为地图。它将对您更具可读性和透明性
  • “我”从何而来?你的意思是“索引”吗?

标签: javascript arrays angular typescript


【解决方案1】:

Typescript 推送到数组中的特定键

你需要在数组上使用splice方法https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

【讨论】:

  • 他正在尝试访问未编入索引的键
  • 我试过 splice ... this.guestPush['filter_' + i].splice(1, 0, users); 但它给了我同样的错误。 VM3282:27 ERROR TypeError: _this.guestPush[("filter_" + i)].splice is not a function at SafeSubscriber._next
  • @CiprianCirstea 你的this.guestPush 是一个数组,你为什么要使用string type 的索引,那真的存在吗? users 也是 object,您正试图将其用作数组。
  • 那么我应该使用什么索引类型。我尝试使用数字类型,但我遇到了困难。我会再试一次。
【解决方案2】:

我最终使用了

 this.guestPush.push({ [i]: { [key]: obj } });

【讨论】:

    【解决方案3】:

    看起来你得到了一个数组响应并且你正在将它分配给对象:

    this.guestPush['filter_' + i] = users;
    

    所以this.guestPush['filter_'+ i] 现在是数组类型。如果你想向它添加另一个 users 对象,你实际上是在向现有数组添加另一个数组。在那种情况下,您不应该使用 concat 吗?

    if (this.guestPush['filter_' + i]) {
        this.guestPush['filter_' + i].concat(users);
    } else {
        this.guestPush['filter_' + i] = users;
    }
    

    如果users不是一个数组,那么它应该是

    if (this.guestPush['filter_' + i]) {
        this.guestPush['filter_' + i].push(users);
    } else {
        this.guestPush['filter_' + i] = [users];
    }
    

    【讨论】:

      【解决方案4】:

      @Ciprian :根据您的回答。

      您需要对代码稍作改动才能使用。我希望下面的代码对你来说足够了。

      let key ='filter_';
      this.guestPush.push({ key + i : users });
      

      【讨论】:

      • 我需要在保存到数据库时将键添加为对象键。每个代表一个用户。我还需要删除基于i的对象
      【解决方案5】:

      这是你的问题,

      guestPush: any[] = new Array;
      'filter_' + i // this will yield a string.
      this.guestPush['filter_' + i])
      

      我可以定义一个对象:

      let name = {};
      name['x'] = 'blah';
      or name.x = 'blahblah'; // name.x does same thing as name['x']
      

      我认为你的想法 name['x'] = 'blah'; 是一个数组,当它是对象语法时。

      因此,您需要考虑实施Dictionary

      【讨论】:

        猜你喜欢
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        • 2017-10-07
        • 2021-03-29
        相关资源
        最近更新 更多