将多个数据发送到同一个数组

Sending multiple data to the same array

我想在数组中生成 JSON 数据以传递给另一个组件。为此,我采用了如下方法。但我想我不是很好。我是这个行业的新手,正在努力学习。

employeeMoney: any[] = [];
employeeId: any[] = [];
    
this.employeeMoney.push(event.target.value);
this.employeeId.push(employId);

这就是我将用户输入的数据抛出的方式。我正在将数据推送到我在同一函数中创建的数组中。

    this.employeeMoney.map(function(item) {
      blopp.all.money.push(item);
    });
    this.employeeId.map(function(item) {
      blopp.all.id.push(item);
    });
    let blopp: any = {
      all: [{id: '', money:''}]
    };

我的目标是将来自 2 个不同数组列表的数据收集到一个列表中。然后我想使用我在此单个列表中收集的数据以我想要的格式生成 JSON 数据。但是在这里我遇到了一个错误。当您在输入中输入数据时。控制台中弹出此错误。

ERROR ReferenceError: Cannot access 'blopp' before initialization

我无法解决这个错误。我正在尝试使用最新的货币和 ID 数据创建这样一个 JSON 结构。

    blopp.map(function(item: any) {
      blopp.money.push({
        "employee_id" : '',
        "amount"  : item,
        "currency"       : 'USD'
      })
    });

但是我这里有一个问题,如何将同一个数组列表中的2个不同数据打印到同一个JSON结构中。

假设 employeeMoneyemployeeId 将始终具有相同的顺序和长度,如果它们以某种方式连接。这是一种可能的解决方案。

const length = employeeId.length;
for (let index = 0; index < length; index++) {
  blopp.all.push({id: employeeId[index], money: employeeMoney[index]})
}

但附带说明一下,我不知道你到底在做什么,但我有一种不好的预感。也许您的系统设计有问题?

首先你必须修复类型,因为你将它们声明为字符串并将数据结构放入其中。

其次,您不能像在 blopp.all.money.push(item);

上那样直接推送数组 属性

这是答案,因为 blopp 是一个对象,其中包含类型为 {id: '', money: ''} 的对象数组,当您推送时,您应该执行以下操作: blopp.all.push({id: 1, money: '1000'})