I'm facing a simple problem that I just can't seem to solve. My goal is to extract data from a service and store it in an array, but no matter what I try, I can't get the desired outcome using properties or arrays.
For example:
abc~sdfgsdg|def~dgdfgdf|cvx~fgdfgfdh|
Sample Code:
let exampleText: string = 'abc~sdfgsdg|def~dgdfgdf|cvx~fgdfgfdh|'
let test: [string, string][];
let test2 = exampleTest.split('|');
test2.forEach(element => {
let test3 = element.split('~');
let t6 = test3[0]
let t8 = test3[1]
test.push(t6,t8)
});
Error:
Argument of type 'string' is not assignable to parameter of type '[string, string]'.ts(2345)
An alternative approach:
let exampleText: string = 'abc~sdfgsdg|def~dgdfgdf|cvx~fgdfgfdh|'
let test: [Pro1:string,Pro2:string];
let test2 = exampleTest.split('|');
test2.forEach(element => {
let test3 = element.split('~');
let t6 = test3[0]
let t8 = test3[1]
test.push(t6,t8)
});
Error:
TypeError: Cannot read properties of undefined (reading 'push')
The desired result:
console.log(test[0][0]) //print 'abc'
console.log(test[0][1]) //print 'sdfgsdg'
console.log(test[1][0]) //print 'def'
console.log(test[1][1]) //print 'dgdfgdf'
Or
console.log(test[0].Pro1) //print 'abc'
console.log(test[0].Pro2) //print 'sdfgsdg'
console.log(test[1].Pro1) //print 'def'
console.log(test[1].Pro2) //print 'dgdfgdf'