How do I parse a string to number while destructuring?












9















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 this:



let input = {latitude: "17.0009", longitude: "82.2108"}


Where latitude and longitude key values are strings, but I want to parse them into a number while destructuring.






let input = {latitude: "17.0009", longitude: "82.2108"}
let {latitude,longitude} = input

console.log(typeof latitude,typeof longitude)





I can see in babel repl that this takes a reference of an object and then access each key. So the above code is the same as:



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = arr.latitude,
longitude = arr.longitude;


I want do something like using the destructuring syntax itself.



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = Number(arr.latitude),
longitude = Number(arr.longitude);


I am open to see some hacks too.



Update



I am able to come with up one hack with the , operator:






let arr = {latitude: "17.0009", longitude: "82.2108"}

let {lat,lng} = ({latitude,longitude} = arr, ({lat:+latitude,lng:+longitude}))

console.log(typeof lat, typeof lng)












share|improve this question

























  • let input = {latitude: "17.0009"- 0, longitude: "82.2108"- 0} While actually destructuring expressions aren't allowed only assignments. Before or after is allowed...

    – zer00ne
    yesterday













  • @zer00ne but i am not able to change input, for example take it as if it is coming from a third party.

    – Code Maniac
    yesterday











  • How about input as a return of a function?

    – zer00ne
    yesterday











  • take this as reference what i am looking to do is {lat:+lat,lng:+lng} to just {lat,lng} if i am able to destructure as well as change parse it to number

    – Code Maniac
    yesterday











  • It looks like there's a type conversion on return

    – zer00ne
    yesterday
















9















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 this:



let input = {latitude: "17.0009", longitude: "82.2108"}


Where latitude and longitude key values are strings, but I want to parse them into a number while destructuring.






let input = {latitude: "17.0009", longitude: "82.2108"}
let {latitude,longitude} = input

console.log(typeof latitude,typeof longitude)





I can see in babel repl that this takes a reference of an object and then access each key. So the above code is the same as:



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = arr.latitude,
longitude = arr.longitude;


I want do something like using the destructuring syntax itself.



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = Number(arr.latitude),
longitude = Number(arr.longitude);


I am open to see some hacks too.



Update



I am able to come with up one hack with the , operator:






let arr = {latitude: "17.0009", longitude: "82.2108"}

let {lat,lng} = ({latitude,longitude} = arr, ({lat:+latitude,lng:+longitude}))

console.log(typeof lat, typeof lng)












share|improve this question

























  • let input = {latitude: "17.0009"- 0, longitude: "82.2108"- 0} While actually destructuring expressions aren't allowed only assignments. Before or after is allowed...

    – zer00ne
    yesterday













  • @zer00ne but i am not able to change input, for example take it as if it is coming from a third party.

    – Code Maniac
    yesterday











  • How about input as a return of a function?

    – zer00ne
    yesterday











  • take this as reference what i am looking to do is {lat:+lat,lng:+lng} to just {lat,lng} if i am able to destructure as well as change parse it to number

    – Code Maniac
    yesterday











  • It looks like there's a type conversion on return

    – zer00ne
    yesterday














9












9








9


3






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 this:



let input = {latitude: "17.0009", longitude: "82.2108"}


Where latitude and longitude key values are strings, but I want to parse them into a number while destructuring.






let input = {latitude: "17.0009", longitude: "82.2108"}
let {latitude,longitude} = input

console.log(typeof latitude,typeof longitude)





I can see in babel repl that this takes a reference of an object and then access each key. So the above code is the same as:



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = arr.latitude,
longitude = arr.longitude;


I want do something like using the destructuring syntax itself.



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = Number(arr.latitude),
longitude = Number(arr.longitude);


I am open to see some hacks too.



Update



I am able to come with up one hack with the , operator:






let arr = {latitude: "17.0009", longitude: "82.2108"}

let {lat,lng} = ({latitude,longitude} = arr, ({lat:+latitude,lng:+longitude}))

console.log(typeof lat, typeof lng)












share|improve this question
















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 this:



let input = {latitude: "17.0009", longitude: "82.2108"}


Where latitude and longitude key values are strings, but I want to parse them into a number while destructuring.






let input = {latitude: "17.0009", longitude: "82.2108"}
let {latitude,longitude} = input

console.log(typeof latitude,typeof longitude)





I can see in babel repl that this takes a reference of an object and then access each key. So the above code is the same as:



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = arr.latitude,
longitude = arr.longitude;


I want do something like using the destructuring syntax itself.



"use strict";

var arr = {
latitude: "17.0009",
longitude: "82.2108"
};
var latitude = Number(arr.latitude),
longitude = Number(arr.longitude);


I am open to see some hacks too.



Update



I am able to come with up one hack with the , operator:






let arr = {latitude: "17.0009", longitude: "82.2108"}

let {lat,lng} = ({latitude,longitude} = arr, ({lat:+latitude,lng:+longitude}))

console.log(typeof lat, typeof lng)








let input = {latitude: "17.0009", longitude: "82.2108"}
let {latitude,longitude} = input

console.log(typeof latitude,typeof longitude)





let input = {latitude: "17.0009", longitude: "82.2108"}
let {latitude,longitude} = input

console.log(typeof latitude,typeof longitude)





let arr = {latitude: "17.0009", longitude: "82.2108"}

let {lat,lng} = ({latitude,longitude} = arr, ({lat:+latitude,lng:+longitude}))

console.log(typeof lat, typeof lng)





let arr = {latitude: "17.0009", longitude: "82.2108"}

let {lat,lng} = ({latitude,longitude} = arr, ({lat:+latitude,lng:+longitude}))

console.log(typeof lat, typeof lng)






javascript ecmascript-6 destructuring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday







Code Maniac

















asked yesterday









Code ManiacCode Maniac

9,7822732




9,7822732













  • let input = {latitude: "17.0009"- 0, longitude: "82.2108"- 0} While actually destructuring expressions aren't allowed only assignments. Before or after is allowed...

    – zer00ne
    yesterday













  • @zer00ne but i am not able to change input, for example take it as if it is coming from a third party.

    – Code Maniac
    yesterday











  • How about input as a return of a function?

    – zer00ne
    yesterday











  • take this as reference what i am looking to do is {lat:+lat,lng:+lng} to just {lat,lng} if i am able to destructure as well as change parse it to number

    – Code Maniac
    yesterday











  • It looks like there's a type conversion on return

    – zer00ne
    yesterday



















  • let input = {latitude: "17.0009"- 0, longitude: "82.2108"- 0} While actually destructuring expressions aren't allowed only assignments. Before or after is allowed...

    – zer00ne
    yesterday













  • @zer00ne but i am not able to change input, for example take it as if it is coming from a third party.

    – Code Maniac
    yesterday











  • How about input as a return of a function?

    – zer00ne
    yesterday











  • take this as reference what i am looking to do is {lat:+lat,lng:+lng} to just {lat,lng} if i am able to destructure as well as change parse it to number

    – Code Maniac
    yesterday











  • It looks like there's a type conversion on return

    – zer00ne
    yesterday

















let input = {latitude: "17.0009"- 0, longitude: "82.2108"- 0} While actually destructuring expressions aren't allowed only assignments. Before or after is allowed...

– zer00ne
yesterday







let input = {latitude: "17.0009"- 0, longitude: "82.2108"- 0} While actually destructuring expressions aren't allowed only assignments. Before or after is allowed...

– zer00ne
yesterday















@zer00ne but i am not able to change input, for example take it as if it is coming from a third party.

– Code Maniac
yesterday





@zer00ne but i am not able to change input, for example take it as if it is coming from a third party.

– Code Maniac
yesterday













How about input as a return of a function?

– zer00ne
yesterday





How about input as a return of a function?

– zer00ne
yesterday













take this as reference what i am looking to do is {lat:+lat,lng:+lng} to just {lat,lng} if i am able to destructure as well as change parse it to number

– Code Maniac
yesterday





take this as reference what i am looking to do is {lat:+lat,lng:+lng} to just {lat,lng} if i am able to destructure as well as change parse it to number

– Code Maniac
yesterday













It looks like there's a type conversion on return

– zer00ne
yesterday





It looks like there's a type conversion on return

– zer00ne
yesterday












7 Answers
7






active

oldest

votes


















8














Destructuring is just a nice way to unpack properties from objects and arrays and assign them to variables. So, any kind of operation is not possible.



One hack would be to create 2 more variables (which don't exist in input) and set the default value to the number equivalent of the previously destrucutred properties:






let input = { latitude: "17.0009", longitude: "82.2108" }
let { latitude, longitude, lat = +latitude, long = +longitude } = input

console.log(typeof latitude, typeof longitude, typeof lat, typeof long)








share|improve this answer



















  • 2





    Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

    – Code Maniac
    yesterday













  • so much upvotes, but horrible to read something like that in a code base.

    – Nina Scholz
    yesterday






  • 2





    @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

    – adiga
    yesterday






  • 1





    It'd be nice if we could just let { +latitude } = …

    – Alhadis
    yesterday





















3














Whilst you cannot perform type conversion within the destructuring expression itself, a possible alternative/workaround could be to destructure the properties within the arguments of a function, and then return an array with the new types within it.



For example, something like the following:






const input = {latitude: "17.0009", longitude: "82.2108"}
const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

console.log(typeof lat, typeof lng); // number number





However, for something like this, I wouldn't use destructuring and probably would resort to regular dot notation:






const input = {latitude: "17.0009", longitude: "82.2108"}
const lat = +input.latitude;
const lng = +input.longitude;

console.log(typeof lat, typeof lng); // number number








share|improve this answer


























  • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

    – Code Maniac
    yesterday








  • 1





    Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

    – jayarjo
    yesterday








  • 1





    @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

    – Nick Parsons
    yesterday






  • 1





    @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

    – Code Maniac
    yesterday



















3














You could have a reusable function, like this:



const numberInputs = input =>
Object.keys(input).reduce((acc, val) => {
acc[val] = +input[val];
return acc;
}, {});


and then reuse it across...



Then do:



let {latitude,longitude} = numberInputs(input);

console.log(typeof latitude,typeof longitude) //number //number


and get 17.0009 and 82.2108 as numbers...



This way you keep your original object also and make a copy... so you have both original and copy of the object which has numbers as values...






share|improve this answer


























  • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

    – Code Maniac
    yesterday



















2














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 valk 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);








share|improve this answer

































    1














    There is a super hacky way of doing this using a getter defined on String.prototype as a helper function.



    (You probably don't want to do that)






    Object.defineProperty (String.prototype, "asNumber",{
    get: function () { return +this}
    });
    let input = {latitude: "17.0009", longitude: "82.2108"}
    let {latitude:{asNumber:latitude},
    longitude: {asNumber:longitude}} = input

    console.log (latitude, longitude)





    Let's break that down into simpler steps.



    //Extending the `String` prototype means every string 
    //will have access to the defined property via
    //its prototype, so
    String.prototype.foo = function () {return `${this}.foo`}
    //means you can now call foo() like any other string method
    "bar".foo() //"bar.foo"`

    //A getter allows you to define a function that acts
    //as a property which will be executed upon access.
    let obj = {get getter () {console.log ('Hi');}}
    obj.getter // Hi

    //Combine those two and you can call functions by
    //accessing properties of strings.
    Object.defineProperty (String.prototype, "asNumber",{
    get: function () { return +this}
    });

    //Now that you have a property that is available at
    //every string - and make it execute a function; you
    //can convert a string to a number, simply by
    //accessing a property
    "42".asNumber //42

    //To make that work with destructuring assignment,
    //you need to know about another handy feature. You
    //can assign destructured properties to a new
    //variable name.
    let {a:b, b:a} = {a:'a', b:'b'};
    a; //'b'
    b; //'a'

    //Since you can nest destructuring assignments, and
    //every string implicitly has a 'asNumber' property,
    //you can destructure that property as well.

    let {lat: {asNumber}} = {lat: "42"};
    asNumber //42

    //The last thing to know is, there's apparently
    //nothing wrong with using an existing variable as
    //new name for a destructured property. So you can
    //just use the `asNumber` property from the
    //prototype and assign it to the same variable
    //you destructured from the object.
    let {lat: {asNumber: lat}} = {lat: "42"};
    lat; //42


    There is nothing wrong with using the same name because only the last variable name will be introduced in the let block's scope






    share|improve this answer


























    • Yes i don't want, but still love see some more explanation about it

      – Code Maniac
      yesterday






    • 1





      @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

      – Moritz Roessler
      yesterday











    • Can't ask for any better explanation :) this explanation contains so much of knowledge

      – Code Maniac
      yesterday











    • But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

      – Code Maniac
      yesterday













    • @CodeManiac Yes, exactly, they are the same! (:

      – Moritz Roessler
      yesterday



















    0














    It's not possible - no operations can be performed on a property during destructuring. If you use destructuring to extract a property into a variable, that variable will be === to the original property value.



    (of course, you could transform the original object's values to Number prior to destructuring, but that's not the same thing)






    share|improve this answer
























    • Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

      – Code Maniac
      yesterday



















    0














    I would probably set things up so that each "object type" I cared about had a corresponding "parser type": an object with the same keys, but whose values are the appropriate parsing functions for each member.



    Like so:






    "use strict";

    var arr = {
    latitude: "17.0009",
    longitude: "82.2108"
    };

    function Parser(propParsers)
    {
    this.propParsers = propParsers;
    this.parse = function (obj) {
    var result = {};
    var propParsers = this.propParsers;
    Object.keys(obj).forEach(function (k) {
    result[k] = propParsers[k](obj[k]);
    });
    return result;
    };
    }

    var parser = new Parser({
    latitude: Number,
    longitude: Number
    });

    let {latitude,longitude} = parser.parse(arr);
    console.log(latitude);
    console.log(longitude);








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55194118%2fhow-do-i-parse-a-string-to-number-while-destructuring%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      Destructuring is just a nice way to unpack properties from objects and arrays and assign them to variables. So, any kind of operation is not possible.



      One hack would be to create 2 more variables (which don't exist in input) and set the default value to the number equivalent of the previously destrucutred properties:






      let input = { latitude: "17.0009", longitude: "82.2108" }
      let { latitude, longitude, lat = +latitude, long = +longitude } = input

      console.log(typeof latitude, typeof longitude, typeof lat, typeof long)








      share|improve this answer



















      • 2





        Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

        – Code Maniac
        yesterday













      • so much upvotes, but horrible to read something like that in a code base.

        – Nina Scholz
        yesterday






      • 2





        @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

        – adiga
        yesterday






      • 1





        It'd be nice if we could just let { +latitude } = …

        – Alhadis
        yesterday


















      8














      Destructuring is just a nice way to unpack properties from objects and arrays and assign them to variables. So, any kind of operation is not possible.



      One hack would be to create 2 more variables (which don't exist in input) and set the default value to the number equivalent of the previously destrucutred properties:






      let input = { latitude: "17.0009", longitude: "82.2108" }
      let { latitude, longitude, lat = +latitude, long = +longitude } = input

      console.log(typeof latitude, typeof longitude, typeof lat, typeof long)








      share|improve this answer



















      • 2





        Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

        – Code Maniac
        yesterday













      • so much upvotes, but horrible to read something like that in a code base.

        – Nina Scholz
        yesterday






      • 2





        @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

        – adiga
        yesterday






      • 1





        It'd be nice if we could just let { +latitude } = …

        – Alhadis
        yesterday
















      8












      8








      8







      Destructuring is just a nice way to unpack properties from objects and arrays and assign them to variables. So, any kind of operation is not possible.



      One hack would be to create 2 more variables (which don't exist in input) and set the default value to the number equivalent of the previously destrucutred properties:






      let input = { latitude: "17.0009", longitude: "82.2108" }
      let { latitude, longitude, lat = +latitude, long = +longitude } = input

      console.log(typeof latitude, typeof longitude, typeof lat, typeof long)








      share|improve this answer













      Destructuring is just a nice way to unpack properties from objects and arrays and assign them to variables. So, any kind of operation is not possible.



      One hack would be to create 2 more variables (which don't exist in input) and set the default value to the number equivalent of the previously destrucutred properties:






      let input = { latitude: "17.0009", longitude: "82.2108" }
      let { latitude, longitude, lat = +latitude, long = +longitude } = input

      console.log(typeof latitude, typeof longitude, typeof lat, typeof long)








      let input = { latitude: "17.0009", longitude: "82.2108" }
      let { latitude, longitude, lat = +latitude, long = +longitude } = input

      console.log(typeof latitude, typeof longitude, typeof lat, typeof long)





      let input = { latitude: "17.0009", longitude: "82.2108" }
      let { latitude, longitude, lat = +latitude, long = +longitude } = input

      console.log(typeof latitude, typeof longitude, typeof lat, typeof long)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered yesterday









      adigaadiga

      11.2k62544




      11.2k62544








      • 2





        Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

        – Code Maniac
        yesterday













      • so much upvotes, but horrible to read something like that in a code base.

        – Nina Scholz
        yesterday






      • 2





        @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

        – adiga
        yesterday






      • 1





        It'd be nice if we could just let { +latitude } = …

        – Alhadis
        yesterday
















      • 2





        Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

        – Code Maniac
        yesterday













      • so much upvotes, but horrible to read something like that in a code base.

        – Nina Scholz
        yesterday






      • 2





        @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

        – adiga
        yesterday






      • 1





        It'd be nice if we could just let { +latitude } = …

        – Alhadis
        yesterday










      2




      2





      Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

      – Code Maniac
      yesterday







      Thanks for idea mate :) yeah that's what i did but with , operator which is less readable

      – Code Maniac
      yesterday















      so much upvotes, but horrible to read something like that in a code base.

      – Nina Scholz
      yesterday





      so much upvotes, but horrible to read something like that in a code base.

      – Nina Scholz
      yesterday




      2




      2





      @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

      – adiga
      yesterday





      @NinaScholz yes, I would never pus this in my code. But OP did ask for hacks

      – adiga
      yesterday




      1




      1





      It'd be nice if we could just let { +latitude } = …

      – Alhadis
      yesterday







      It'd be nice if we could just let { +latitude } = …

      – Alhadis
      yesterday















      3














      Whilst you cannot perform type conversion within the destructuring expression itself, a possible alternative/workaround could be to destructure the properties within the arguments of a function, and then return an array with the new types within it.



      For example, something like the following:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

      console.log(typeof lat, typeof lng); // number number





      However, for something like this, I wouldn't use destructuring and probably would resort to regular dot notation:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const lat = +input.latitude;
      const lng = +input.longitude;

      console.log(typeof lat, typeof lng); // number number








      share|improve this answer


























      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday








      • 1





        Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

        – jayarjo
        yesterday








      • 1





        @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

        – Nick Parsons
        yesterday






      • 1





        @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

        – Code Maniac
        yesterday
















      3














      Whilst you cannot perform type conversion within the destructuring expression itself, a possible alternative/workaround could be to destructure the properties within the arguments of a function, and then return an array with the new types within it.



      For example, something like the following:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

      console.log(typeof lat, typeof lng); // number number





      However, for something like this, I wouldn't use destructuring and probably would resort to regular dot notation:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const lat = +input.latitude;
      const lng = +input.longitude;

      console.log(typeof lat, typeof lng); // number number








      share|improve this answer


























      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday








      • 1





        Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

        – jayarjo
        yesterday








      • 1





        @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

        – Nick Parsons
        yesterday






      • 1





        @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

        – Code Maniac
        yesterday














      3












      3








      3







      Whilst you cannot perform type conversion within the destructuring expression itself, a possible alternative/workaround could be to destructure the properties within the arguments of a function, and then return an array with the new types within it.



      For example, something like the following:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

      console.log(typeof lat, typeof lng); // number number





      However, for something like this, I wouldn't use destructuring and probably would resort to regular dot notation:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const lat = +input.latitude;
      const lng = +input.longitude;

      console.log(typeof lat, typeof lng); // number number








      share|improve this answer















      Whilst you cannot perform type conversion within the destructuring expression itself, a possible alternative/workaround could be to destructure the properties within the arguments of a function, and then return an array with the new types within it.



      For example, something like the following:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

      console.log(typeof lat, typeof lng); // number number





      However, for something like this, I wouldn't use destructuring and probably would resort to regular dot notation:






      const input = {latitude: "17.0009", longitude: "82.2108"}
      const lat = +input.latitude;
      const lng = +input.longitude;

      console.log(typeof lat, typeof lng); // number number








      const input = {latitude: "17.0009", longitude: "82.2108"}
      const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

      console.log(typeof lat, typeof lng); // number number





      const input = {latitude: "17.0009", longitude: "82.2108"}
      const [lat, lng] = (({latitude:a, longitude:b}) => [+a, +b])(input);

      console.log(typeof lat, typeof lng); // number number





      const input = {latitude: "17.0009", longitude: "82.2108"}
      const lat = +input.latitude;
      const lng = +input.longitude;

      console.log(typeof lat, typeof lng); // number number





      const input = {latitude: "17.0009", longitude: "82.2108"}
      const lat = +input.latitude;
      const lng = +input.longitude;

      console.log(typeof lat, typeof lng); // number number






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited yesterday

























      answered yesterday









      Nick ParsonsNick Parsons

      9,9212926




      9,9212926













      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday








      • 1





        Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

        – jayarjo
        yesterday








      • 1





        @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

        – Nick Parsons
        yesterday






      • 1





        @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

        – Code Maniac
        yesterday



















      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday








      • 1





        Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

        – jayarjo
        yesterday








      • 1





        @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

        – Nick Parsons
        yesterday






      • 1





        @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

        – Code Maniac
        yesterday

















      Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

      – Code Maniac
      yesterday







      Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

      – Code Maniac
      yesterday






      1




      1





      Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

      – jayarjo
      yesterday







      Favor readability over hacky shortcuts - other users of your code and future you will thank you. Modern transpilers will do all the minification for you. So why not write in human readable form?

      – jayarjo
      yesterday






      1




      1





      @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

      – Nick Parsons
      yesterday





      @CodeManiac hm, I couldn't come up with anything, but it seems like adiga has, I think their solution is what you're after

      – Nick Parsons
      yesterday




      1




      1





      @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

      – Code Maniac
      yesterday





      @NickParsons yeah mate his answer is cleaner than the , operator hack which i was doing

      – Code Maniac
      yesterday











      3














      You could have a reusable function, like this:



      const numberInputs = input =>
      Object.keys(input).reduce((acc, val) => {
      acc[val] = +input[val];
      return acc;
      }, {});


      and then reuse it across...



      Then do:



      let {latitude,longitude} = numberInputs(input);

      console.log(typeof latitude,typeof longitude) //number //number


      and get 17.0009 and 82.2108 as numbers...



      This way you keep your original object also and make a copy... so you have both original and copy of the object which has numbers as values...






      share|improve this answer


























      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday
















      3














      You could have a reusable function, like this:



      const numberInputs = input =>
      Object.keys(input).reduce((acc, val) => {
      acc[val] = +input[val];
      return acc;
      }, {});


      and then reuse it across...



      Then do:



      let {latitude,longitude} = numberInputs(input);

      console.log(typeof latitude,typeof longitude) //number //number


      and get 17.0009 and 82.2108 as numbers...



      This way you keep your original object also and make a copy... so you have both original and copy of the object which has numbers as values...






      share|improve this answer


























      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday














      3












      3








      3







      You could have a reusable function, like this:



      const numberInputs = input =>
      Object.keys(input).reduce((acc, val) => {
      acc[val] = +input[val];
      return acc;
      }, {});


      and then reuse it across...



      Then do:



      let {latitude,longitude} = numberInputs(input);

      console.log(typeof latitude,typeof longitude) //number //number


      and get 17.0009 and 82.2108 as numbers...



      This way you keep your original object also and make a copy... so you have both original and copy of the object which has numbers as values...






      share|improve this answer















      You could have a reusable function, like this:



      const numberInputs = input =>
      Object.keys(input).reduce((acc, val) => {
      acc[val] = +input[val];
      return acc;
      }, {});


      and then reuse it across...



      Then do:



      let {latitude,longitude} = numberInputs(input);

      console.log(typeof latitude,typeof longitude) //number //number


      and get 17.0009 and 82.2108 as numbers...



      This way you keep your original object also and make a copy... so you have both original and copy of the object which has numbers as values...







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 22 hours ago

























      answered yesterday









      AlirezaAlireza

      51.1k13175123




      51.1k13175123













      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday



















      • Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

        – Code Maniac
        yesterday

















      Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

      – Code Maniac
      yesterday





      Thanks for inputs. i am able to do it with out function using , operator and destructuring as you can see in the update. but i am having hard time to incorporate the in map arr.map((/Here i am trying to do the same what i am able to do with comma operatore/) => {}) can you help in that ?

      – Code Maniac
      yesterday











      2














      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 valk 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);








      share|improve this answer






























        2














        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 valk 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);








        share|improve this answer




























          2












          2








          2







          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 valk 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);








          share|improve this answer















          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 valk 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);








          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);





          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);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Nina ScholzNina Scholz

          191k15104176




          191k15104176























              1














              There is a super hacky way of doing this using a getter defined on String.prototype as a helper function.



              (You probably don't want to do that)






              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });
              let input = {latitude: "17.0009", longitude: "82.2108"}
              let {latitude:{asNumber:latitude},
              longitude: {asNumber:longitude}} = input

              console.log (latitude, longitude)





              Let's break that down into simpler steps.



              //Extending the `String` prototype means every string 
              //will have access to the defined property via
              //its prototype, so
              String.prototype.foo = function () {return `${this}.foo`}
              //means you can now call foo() like any other string method
              "bar".foo() //"bar.foo"`

              //A getter allows you to define a function that acts
              //as a property which will be executed upon access.
              let obj = {get getter () {console.log ('Hi');}}
              obj.getter // Hi

              //Combine those two and you can call functions by
              //accessing properties of strings.
              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });

              //Now that you have a property that is available at
              //every string - and make it execute a function; you
              //can convert a string to a number, simply by
              //accessing a property
              "42".asNumber //42

              //To make that work with destructuring assignment,
              //you need to know about another handy feature. You
              //can assign destructured properties to a new
              //variable name.
              let {a:b, b:a} = {a:'a', b:'b'};
              a; //'b'
              b; //'a'

              //Since you can nest destructuring assignments, and
              //every string implicitly has a 'asNumber' property,
              //you can destructure that property as well.

              let {lat: {asNumber}} = {lat: "42"};
              asNumber //42

              //The last thing to know is, there's apparently
              //nothing wrong with using an existing variable as
              //new name for a destructured property. So you can
              //just use the `asNumber` property from the
              //prototype and assign it to the same variable
              //you destructured from the object.
              let {lat: {asNumber: lat}} = {lat: "42"};
              lat; //42


              There is nothing wrong with using the same name because only the last variable name will be introduced in the let block's scope






              share|improve this answer


























              • Yes i don't want, but still love see some more explanation about it

                – Code Maniac
                yesterday






              • 1





                @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

                – Moritz Roessler
                yesterday











              • Can't ask for any better explanation :) this explanation contains so much of knowledge

                – Code Maniac
                yesterday











              • But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

                – Code Maniac
                yesterday













              • @CodeManiac Yes, exactly, they are the same! (:

                – Moritz Roessler
                yesterday
















              1














              There is a super hacky way of doing this using a getter defined on String.prototype as a helper function.



              (You probably don't want to do that)






              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });
              let input = {latitude: "17.0009", longitude: "82.2108"}
              let {latitude:{asNumber:latitude},
              longitude: {asNumber:longitude}} = input

              console.log (latitude, longitude)





              Let's break that down into simpler steps.



              //Extending the `String` prototype means every string 
              //will have access to the defined property via
              //its prototype, so
              String.prototype.foo = function () {return `${this}.foo`}
              //means you can now call foo() like any other string method
              "bar".foo() //"bar.foo"`

              //A getter allows you to define a function that acts
              //as a property which will be executed upon access.
              let obj = {get getter () {console.log ('Hi');}}
              obj.getter // Hi

              //Combine those two and you can call functions by
              //accessing properties of strings.
              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });

              //Now that you have a property that is available at
              //every string - and make it execute a function; you
              //can convert a string to a number, simply by
              //accessing a property
              "42".asNumber //42

              //To make that work with destructuring assignment,
              //you need to know about another handy feature. You
              //can assign destructured properties to a new
              //variable name.
              let {a:b, b:a} = {a:'a', b:'b'};
              a; //'b'
              b; //'a'

              //Since you can nest destructuring assignments, and
              //every string implicitly has a 'asNumber' property,
              //you can destructure that property as well.

              let {lat: {asNumber}} = {lat: "42"};
              asNumber //42

              //The last thing to know is, there's apparently
              //nothing wrong with using an existing variable as
              //new name for a destructured property. So you can
              //just use the `asNumber` property from the
              //prototype and assign it to the same variable
              //you destructured from the object.
              let {lat: {asNumber: lat}} = {lat: "42"};
              lat; //42


              There is nothing wrong with using the same name because only the last variable name will be introduced in the let block's scope






              share|improve this answer


























              • Yes i don't want, but still love see some more explanation about it

                – Code Maniac
                yesterday






              • 1





                @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

                – Moritz Roessler
                yesterday











              • Can't ask for any better explanation :) this explanation contains so much of knowledge

                – Code Maniac
                yesterday











              • But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

                – Code Maniac
                yesterday













              • @CodeManiac Yes, exactly, they are the same! (:

                – Moritz Roessler
                yesterday














              1












              1








              1







              There is a super hacky way of doing this using a getter defined on String.prototype as a helper function.



              (You probably don't want to do that)






              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });
              let input = {latitude: "17.0009", longitude: "82.2108"}
              let {latitude:{asNumber:latitude},
              longitude: {asNumber:longitude}} = input

              console.log (latitude, longitude)





              Let's break that down into simpler steps.



              //Extending the `String` prototype means every string 
              //will have access to the defined property via
              //its prototype, so
              String.prototype.foo = function () {return `${this}.foo`}
              //means you can now call foo() like any other string method
              "bar".foo() //"bar.foo"`

              //A getter allows you to define a function that acts
              //as a property which will be executed upon access.
              let obj = {get getter () {console.log ('Hi');}}
              obj.getter // Hi

              //Combine those two and you can call functions by
              //accessing properties of strings.
              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });

              //Now that you have a property that is available at
              //every string - and make it execute a function; you
              //can convert a string to a number, simply by
              //accessing a property
              "42".asNumber //42

              //To make that work with destructuring assignment,
              //you need to know about another handy feature. You
              //can assign destructured properties to a new
              //variable name.
              let {a:b, b:a} = {a:'a', b:'b'};
              a; //'b'
              b; //'a'

              //Since you can nest destructuring assignments, and
              //every string implicitly has a 'asNumber' property,
              //you can destructure that property as well.

              let {lat: {asNumber}} = {lat: "42"};
              asNumber //42

              //The last thing to know is, there's apparently
              //nothing wrong with using an existing variable as
              //new name for a destructured property. So you can
              //just use the `asNumber` property from the
              //prototype and assign it to the same variable
              //you destructured from the object.
              let {lat: {asNumber: lat}} = {lat: "42"};
              lat; //42


              There is nothing wrong with using the same name because only the last variable name will be introduced in the let block's scope






              share|improve this answer















              There is a super hacky way of doing this using a getter defined on String.prototype as a helper function.



              (You probably don't want to do that)






              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });
              let input = {latitude: "17.0009", longitude: "82.2108"}
              let {latitude:{asNumber:latitude},
              longitude: {asNumber:longitude}} = input

              console.log (latitude, longitude)





              Let's break that down into simpler steps.



              //Extending the `String` prototype means every string 
              //will have access to the defined property via
              //its prototype, so
              String.prototype.foo = function () {return `${this}.foo`}
              //means you can now call foo() like any other string method
              "bar".foo() //"bar.foo"`

              //A getter allows you to define a function that acts
              //as a property which will be executed upon access.
              let obj = {get getter () {console.log ('Hi');}}
              obj.getter // Hi

              //Combine those two and you can call functions by
              //accessing properties of strings.
              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });

              //Now that you have a property that is available at
              //every string - and make it execute a function; you
              //can convert a string to a number, simply by
              //accessing a property
              "42".asNumber //42

              //To make that work with destructuring assignment,
              //you need to know about another handy feature. You
              //can assign destructured properties to a new
              //variable name.
              let {a:b, b:a} = {a:'a', b:'b'};
              a; //'b'
              b; //'a'

              //Since you can nest destructuring assignments, and
              //every string implicitly has a 'asNumber' property,
              //you can destructure that property as well.

              let {lat: {asNumber}} = {lat: "42"};
              asNumber //42

              //The last thing to know is, there's apparently
              //nothing wrong with using an existing variable as
              //new name for a destructured property. So you can
              //just use the `asNumber` property from the
              //prototype and assign it to the same variable
              //you destructured from the object.
              let {lat: {asNumber: lat}} = {lat: "42"};
              lat; //42


              There is nothing wrong with using the same name because only the last variable name will be introduced in the let block's scope






              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });
              let input = {latitude: "17.0009", longitude: "82.2108"}
              let {latitude:{asNumber:latitude},
              longitude: {asNumber:longitude}} = input

              console.log (latitude, longitude)





              Object.defineProperty (String.prototype, "asNumber",{
              get: function () { return +this}
              });
              let input = {latitude: "17.0009", longitude: "82.2108"}
              let {latitude:{asNumber:latitude},
              longitude: {asNumber:longitude}} = input

              console.log (latitude, longitude)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited yesterday

























              answered yesterday









              Moritz RoesslerMoritz Roessler

              6,3061747




              6,3061747













              • Yes i don't want, but still love see some more explanation about it

                – Code Maniac
                yesterday






              • 1





                @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

                – Moritz Roessler
                yesterday











              • Can't ask for any better explanation :) this explanation contains so much of knowledge

                – Code Maniac
                yesterday











              • But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

                – Code Maniac
                yesterday













              • @CodeManiac Yes, exactly, they are the same! (:

                – Moritz Roessler
                yesterday



















              • Yes i don't want, but still love see some more explanation about it

                – Code Maniac
                yesterday






              • 1





                @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

                – Moritz Roessler
                yesterday











              • Can't ask for any better explanation :) this explanation contains so much of knowledge

                – Code Maniac
                yesterday











              • But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

                – Code Maniac
                yesterday













              • @CodeManiac Yes, exactly, they are the same! (:

                – Moritz Roessler
                yesterday

















              Yes i don't want, but still love see some more explanation about it

              – Code Maniac
              yesterday





              Yes i don't want, but still love see some more explanation about it

              – Code Maniac
              yesterday




              1




              1





              @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

              – Moritz Roessler
              yesterday





              @CodeManiac Sure (: glad to see people interested in the language. I added an explanation

              – Moritz Roessler
              yesterday













              Can't ask for any better explanation :) this explanation contains so much of knowledge

              – Code Maniac
              yesterday





              Can't ask for any better explanation :) this explanation contains so much of knowledge

              – Code Maniac
              yesterday













              But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

              – Code Maniac
              yesterday







              But one thing i want to know let {a : {b}} = {'a' : {'b': 'some value }} here the nested destructuring captures b as some value. than how let {lat: {asNumber}} = {lat: "42"}; is capturing ? or is it same as let {lat:asNumber} = {lat} = {lat: "42"}; ?

              – Code Maniac
              yesterday















              @CodeManiac Yes, exactly, they are the same! (:

              – Moritz Roessler
              yesterday





              @CodeManiac Yes, exactly, they are the same! (:

              – Moritz Roessler
              yesterday











              0














              It's not possible - no operations can be performed on a property during destructuring. If you use destructuring to extract a property into a variable, that variable will be === to the original property value.



              (of course, you could transform the original object's values to Number prior to destructuring, but that's not the same thing)






              share|improve this answer
























              • Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

                – Code Maniac
                yesterday
















              0














              It's not possible - no operations can be performed on a property during destructuring. If you use destructuring to extract a property into a variable, that variable will be === to the original property value.



              (of course, you could transform the original object's values to Number prior to destructuring, but that's not the same thing)






              share|improve this answer
























              • Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

                – Code Maniac
                yesterday














              0












              0








              0







              It's not possible - no operations can be performed on a property during destructuring. If you use destructuring to extract a property into a variable, that variable will be === to the original property value.



              (of course, you could transform the original object's values to Number prior to destructuring, but that's not the same thing)






              share|improve this answer













              It's not possible - no operations can be performed on a property during destructuring. If you use destructuring to extract a property into a variable, that variable will be === to the original property value.



              (of course, you could transform the original object's values to Number prior to destructuring, but that's not the same thing)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered yesterday









              SnowSnow

              89319




              89319













              • Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

                – Code Maniac
                yesterday



















              • Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

                – Code Maniac
                yesterday

















              Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

              – Code Maniac
              yesterday





              Yeah that's kind of specified in question already. can i see some hacks around if you can come up with any ?

              – Code Maniac
              yesterday











              0














              I would probably set things up so that each "object type" I cared about had a corresponding "parser type": an object with the same keys, but whose values are the appropriate parsing functions for each member.



              Like so:






              "use strict";

              var arr = {
              latitude: "17.0009",
              longitude: "82.2108"
              };

              function Parser(propParsers)
              {
              this.propParsers = propParsers;
              this.parse = function (obj) {
              var result = {};
              var propParsers = this.propParsers;
              Object.keys(obj).forEach(function (k) {
              result[k] = propParsers[k](obj[k]);
              });
              return result;
              };
              }

              var parser = new Parser({
              latitude: Number,
              longitude: Number
              });

              let {latitude,longitude} = parser.parse(arr);
              console.log(latitude);
              console.log(longitude);








              share|improve this answer




























                0














                I would probably set things up so that each "object type" I cared about had a corresponding "parser type": an object with the same keys, but whose values are the appropriate parsing functions for each member.



                Like so:






                "use strict";

                var arr = {
                latitude: "17.0009",
                longitude: "82.2108"
                };

                function Parser(propParsers)
                {
                this.propParsers = propParsers;
                this.parse = function (obj) {
                var result = {};
                var propParsers = this.propParsers;
                Object.keys(obj).forEach(function (k) {
                result[k] = propParsers[k](obj[k]);
                });
                return result;
                };
                }

                var parser = new Parser({
                latitude: Number,
                longitude: Number
                });

                let {latitude,longitude} = parser.parse(arr);
                console.log(latitude);
                console.log(longitude);








                share|improve this answer


























                  0












                  0








                  0







                  I would probably set things up so that each "object type" I cared about had a corresponding "parser type": an object with the same keys, but whose values are the appropriate parsing functions for each member.



                  Like so:






                  "use strict";

                  var arr = {
                  latitude: "17.0009",
                  longitude: "82.2108"
                  };

                  function Parser(propParsers)
                  {
                  this.propParsers = propParsers;
                  this.parse = function (obj) {
                  var result = {};
                  var propParsers = this.propParsers;
                  Object.keys(obj).forEach(function (k) {
                  result[k] = propParsers[k](obj[k]);
                  });
                  return result;
                  };
                  }

                  var parser = new Parser({
                  latitude: Number,
                  longitude: Number
                  });

                  let {latitude,longitude} = parser.parse(arr);
                  console.log(latitude);
                  console.log(longitude);








                  share|improve this answer













                  I would probably set things up so that each "object type" I cared about had a corresponding "parser type": an object with the same keys, but whose values are the appropriate parsing functions for each member.



                  Like so:






                  "use strict";

                  var arr = {
                  latitude: "17.0009",
                  longitude: "82.2108"
                  };

                  function Parser(propParsers)
                  {
                  this.propParsers = propParsers;
                  this.parse = function (obj) {
                  var result = {};
                  var propParsers = this.propParsers;
                  Object.keys(obj).forEach(function (k) {
                  result[k] = propParsers[k](obj[k]);
                  });
                  return result;
                  };
                  }

                  var parser = new Parser({
                  latitude: Number,
                  longitude: Number
                  });

                  let {latitude,longitude} = parser.parse(arr);
                  console.log(latitude);
                  console.log(longitude);








                  "use strict";

                  var arr = {
                  latitude: "17.0009",
                  longitude: "82.2108"
                  };

                  function Parser(propParsers)
                  {
                  this.propParsers = propParsers;
                  this.parse = function (obj) {
                  var result = {};
                  var propParsers = this.propParsers;
                  Object.keys(obj).forEach(function (k) {
                  result[k] = propParsers[k](obj[k]);
                  });
                  return result;
                  };
                  }

                  var parser = new Parser({
                  latitude: Number,
                  longitude: Number
                  });

                  let {latitude,longitude} = parser.parse(arr);
                  console.log(latitude);
                  console.log(longitude);





                  "use strict";

                  var arr = {
                  latitude: "17.0009",
                  longitude: "82.2108"
                  };

                  function Parser(propParsers)
                  {
                  this.propParsers = propParsers;
                  this.parse = function (obj) {
                  var result = {};
                  var propParsers = this.propParsers;
                  Object.keys(obj).forEach(function (k) {
                  result[k] = propParsers[k](obj[k]);
                  });
                  return result;
                  };
                  }

                  var parser = new Parser({
                  latitude: Number,
                  longitude: Number
                  });

                  let {latitude,longitude} = parser.parse(arr);
                  console.log(latitude);
                  console.log(longitude);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Derrick TurkDerrick Turk

                  3,70312126




                  3,70312126






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55194118%2fhow-do-i-parse-a-string-to-number-while-destructuring%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?