How do I parse a string to number while destructuring?
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)
javascript ecmascript-6 destructuring
|
show 2 more comments
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)
javascript ecmascript-6 destructuring
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
|
show 2 more comments
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)
javascript ecmascript-6 destructuring
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
javascript ecmascript-6 destructuring
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
|
show 2 more comments
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
|
show 2 more comments
7 Answers
7
active
oldest
votes
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)
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 justlet { +latitude } = …
– Alhadis
yesterday
add a comment |
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
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
add a comment |
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...
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 maparr.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
add a comment |
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);
add a comment |
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
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 knowlet {a : {b}} = {'a' : {'b': 'some value }}
here the nested destructuring capturesb
assome value
. than howlet {lat: {asNumber}} = {lat: "42"};
is capturing ? or is it same aslet {lat:asNumber} = {lat} = {lat: "42"};
?
– Code Maniac
yesterday
@CodeManiac Yes, exactly, they are the same! (:
– Moritz Roessler
yesterday
|
show 1 more comment
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)
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
add a comment |
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);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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 justlet { +latitude } = …
– Alhadis
yesterday
add a comment |
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)
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 justlet { +latitude } = …
– Alhadis
yesterday
add a comment |
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)
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)
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 justlet { +latitude } = …
– Alhadis
yesterday
add a comment |
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 justlet { +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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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...
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 maparr.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
add a comment |
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...
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 maparr.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
add a comment |
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...
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...
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 maparr.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
add a comment |
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 maparr.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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
edited yesterday
answered yesterday
Nina ScholzNina Scholz
191k15104176
191k15104176
add a comment |
add a comment |
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
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 knowlet {a : {b}} = {'a' : {'b': 'some value }}
here the nested destructuring capturesb
assome value
. than howlet {lat: {asNumber}} = {lat: "42"};
is capturing ? or is it same aslet {lat:asNumber} = {lat} = {lat: "42"};
?
– Code Maniac
yesterday
@CodeManiac Yes, exactly, they are the same! (:
– Moritz Roessler
yesterday
|
show 1 more comment
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
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 knowlet {a : {b}} = {'a' : {'b': 'some value }}
here the nested destructuring capturesb
assome value
. than howlet {lat: {asNumber}} = {lat: "42"};
is capturing ? or is it same aslet {lat:asNumber} = {lat} = {lat: "42"};
?
– Code Maniac
yesterday
@CodeManiac Yes, exactly, they are the same! (:
– Moritz Roessler
yesterday
|
show 1 more comment
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
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)
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 knowlet {a : {b}} = {'a' : {'b': 'some value }}
here the nested destructuring capturesb
assome value
. than howlet {lat: {asNumber}} = {lat: "42"};
is capturing ? or is it same aslet {lat:asNumber} = {lat} = {lat: "42"};
?
– Code Maniac
yesterday
@CodeManiac Yes, exactly, they are the same! (:
– Moritz Roessler
yesterday
|
show 1 more comment
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 knowlet {a : {b}} = {'a' : {'b': 'some value }}
here the nested destructuring capturesb
assome value
. than howlet {lat: {asNumber}} = {lat: "42"};
is capturing ? or is it same aslet {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
|
show 1 more comment
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered yesterday
Derrick TurkDerrick Turk
3,70312126
3,70312126
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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