How do I parse a string to number while destructuring?

前端 未结 8 1032
[愿得一人]
[愿得一人] 2020-12-05 10:23

I am trying to experiment around destructuring assignment. Now I have a case which I trying to cop up with destructuring itself.

For example, I have an input like th

相关标签:
8条回答
  • 2020-12-05 11:04

    If you don't mind using lodash, you can try this:

    import { mapValues } from 'lodash';
    
    const input = {latitude: "17.0009", longitude: "82.2108"}
    const {latitude, longitude} = mapValues(input, Number);
    
    0 讨论(0)
  • 2020-12-05 11:05

    You could destructure the values, take an array of the values and map the a new data type of the value and assign this values back to the variables.

    let input = { latitude: "17.0009", longitude: "82.2108" },
        { latitude, longitude} = input;
    
    [latitude, longitude] = [latitude, longitude].map(Number);
    
    console.log(typeof latitude, latitude);
    console.log(typeof longitude, longitude);

    0 讨论(0)
提交回复
热议问题