I have a string as input :
'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL'
and i need to split the same into Array of Arrays , something like :
[[PARTIALREFUND, 50, SUCCESS],[PARTIALREFUND, 50, FAIL]]
I tired split on '-' , but that gives me an object with 2 keys , is there any other way to achieve this.
If required i can split
Solution Tired but did not worked:
const params = {postAuthActionId: 'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL'};
const result = params.postAuthActionId.split('-').map((s) => s.split(','));
console.log(result);
console.log(typeof result);
CodePudding user response:
split
the string on -
, and when you map
over each element of the array return a new array, coercing the number from a string.
const params = {
postAuthActionId: 'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL'
};
const arr = params.postAuthActionId.split('- ');
const result = arr.map(el => {
const [ id, number, status ] = el.split(', ');
return [id, Number(number), status]
});
console.log(result);
CodePudding user response:
You can simply achieve it by using Array.map() along with spread (...) operator.
Demo :
const str = 'PARTIALREFUND, 50, SUCCESS - PARTIALREFUND, 50, FAIL';
const arr = str.split(' - ');
const res = arr.map(item => {
return [...item.split(', ')]
});
console.log(res);