Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

can i parse string to number while destructuring ? #66

Open
vivekjain202 opened this issue Mar 16, 2019 · 5 comments
Open

can i parse string to number while destructuring ? #66

vivekjain202 opened this issue Mar 16, 2019 · 5 comments

Comments

@vivekjain202
Copy link

vivekjain202 commented Mar 16, 2019

This is just a question.

For example, I have an input like this:

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

Where latitude and longitude key's value 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)
// string string

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);
@vicksiyi
Copy link

This is just a question.

For example, I have an input like this:

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

Where latitude and longitude key's value 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)
// string string

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

Hello, you solved it?

@hax
Copy link
Member

hax commented May 11, 2020

This is a stage 4 proposal which means nothing can be added.

I think this requirement could be move to pattern match proposal and see what we can do.

@vivekjain202
Copy link
Author

This is just a question.
For example, I have an input like this:
let input = {latitude: "17.0009", longitude: "82.2108"}
Where latitude and longitude key's value 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)
// string string

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

Hello, you solved it?

Yes but not the way i wanted, it would have been great if i could have done during destructuring itself but with current implementation it seems not possible.

Here are some answer on the same question i asked on https://stackoverflow.com/questions/55194118/how-do-i-parse-a-string-to-number-while-destructuring , specially this answer https://stackoverflow.com/a/55197490/9624435

@vivekjain202
Copy link
Author

@hax Thanks mate, very new to all this, i just found out that this can be handy in many circumstances so thought of asking here,

Didn't knew about pattern-matching thing before, i'll go through proposal for sure, let me know if there's anything i can contribute as well as a newbie 😁

@vicksiyi
Copy link

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

// Define a function to convert to number
const numberInputs = input =>
    Object.keys(input).reduce((acc, val) => {
        acc[val] = +input[val];
        return acc;
    }, {});
let {latitude,longitude} = numberInputs(arr)

I think that's good.

You can define your criteria in the numberinput function to convert to Number. 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants