Issue
I want to generate JSON data in an array to pass to another component. For this, I followed a method as follows. But I guess I wasn't very good. I'm new to this business and trying to learn.
employeeMoney: any[] = [];
employeeId: any[] = [];
this.employeeMoney.push(event.target.value);
this.employeeId.push(employId);
This is how I throw the data I get from the user with the input. I'm pushing data into this array I created in the same function.
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:''}]
};
My goal here is to collect data from 2 different array lists into a single list. Then I want to generate the JSON data in the format I want using the data I have collected in this single list. But here I am encountering an error. when you enter the data in the input. This error pops up in the console.
ERROR ReferenceError: Cannot access 'blopp' before initialization
i couldn't solve this error. I'm trying to create such a JSON structure using the latest money and id data.
blopp.map(function(item: any) {
blopp.money.push({
"employee_id" : '',
"amount" : item,
"currency" : 'USD'
})
});
But I have a question here, how can I print 2 different data in the same array list to the same JSON structure.
Solution
First of all you have to fix the types since you declare them string and put data structures in them.
Secondly you cannot push directly on array property just like you are doing on: blopp.all.money.push(item);
Here is the answer, since blopp is an object which contains an array of objects of type {id: '', money: ''}
when you push you should do the following:
blopp.all.push({id: 1, money: '1000'})
Answered By - ezhupa99
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.