I have a string like "D-30-25-4", then I want 30 and 25 as the desired result
I have tried this
value = "D-30-25-4";
value1= parseInt(value.substring(5,7)); // 25
value12 = parseInt(value.substring(2,4)); //30
but it fails when value is "D-30-100-4";
split function of String is your friend.
value = "D-30-25-4";
var vals = value.split("-");
value1= vals[2]; // 25
value12 = vals[1]; //30
You could split and slice the string for item at index 1
and 2
.
function parts(s) { return s.split('-').slice(1, 3); } var object = { 2: "D-30-25-4", 3: "D-30-50-4", 4: "D-30-10-4", 15: "D-30-100-4" }; console.log(parts(object[2])); console.log(parts(object[15]));
尝试这个:
let [,value1,value12] = value.split('-').map(parseFloat)
Use split operation. split returns array
var res = {"2":"D-30-25-4", "3":"D-30-50-4", "4":"D-30-10-4", "15":"D-30-100-4"}; for(obj in res){ var result = res[obj].split("-"); // result will have ["D","30","25",4"]; console.log(result[1]); console.log(result[2]); console.log(result[3]); }
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.