简体   繁体   中英

Separate Fraction accordingly using javascript

I can I separate fractions accordingly as numerator and denominator using javascript split.

var str="1/2+2/4";    
str.split('/').split('+');//not Working

Now I want it like num=[1,2] and den=[2,4]

You're trying to split an array.

An alternative is splitting the string by symbol + and then execute the function reduce .

 var str="1/2+2/4+3/4"; var result = str.split('+').reduce((a, c) => { var [num, den] = c.split("/"); a.den.push(den); a.num.push(num); return a; }, {num: [], den: []}); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM