解构时如何将字符串解析为数字? [英] How do I parse a string to number while destructuring?

查看:85
本文介绍了解构时如何将字符串解析为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究破坏性分配.现在我有一个案例,我试图应对自身的破坏.

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"}

其中latitudelongitude键值是字符串,但是我想在解构时将它们解析为数字.

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.

更新

我可以使用,运算符来解决一个问题:

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)

旁注:- ,您必须阅读 莫里茨·罗斯勒的答案这很骇人,但其中包含丰富的知识和信息

On side note:- you must read Moritz Roessler's answer this is hacky but contains good knowledge and information

推荐答案

解构只是从对象和数组解压缩属性并将它们分配给变量的一种好方法.正如问题中的代码所暗示的那样,任何类型的操作都是不可能的.

Destructuring is just a nice way to unpack properties from objects and arrays and assign them to variables. As the trasnpiled code in the question suggests, any kind of operation is not possible.

一个 hack 将是另外创建2个变量(在input中不存在)并设置

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)

代码大约遍历此代码(它只是在利用创建变量和分配属性值的顺序.同样,这仅在input中没有latlong属性的情况下有效.否则,它将失败三元条件,并且lat将设置为input.lat.

It's just exploiting the order in which the variables are created and assigned property values. Again, this works only if there are no lat or long properties in input. Otherwise, it will fail the ternary condition and lat will be set to input.lat.

尽管如此,这样的阅读起来会容易得多:

Something like this would be much easier to read though:

let { latitude, longitude } = input;
let lat = +latitude, 
    long = +longitude;

OR

let [ lat, long ] = [ +latitude, +longitude ]

这篇关于解构时如何将字符串解析为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆