简体   繁体   English

ES6 javascript 在反应中解构大对象

[英]ES6 javascript destructuring large objects in react

I suspect the answer is "you can't" but there are a lot of people here who are brighter than I. In reviewing (and re-writing) code I wrote a year-ish ago (before I learned redux), I have a react component that contains the following destructing of a user profile passed to as a prop as user :我怀疑答案是“你不能”,但这里有很多人比我聪明。在审查(和重写)我一年前(在我学习 redux 之前)编写的代码时,我有一个反应组件,其中包含以下对作为user的 prop 传递的用户配置文件的破坏:

  const {user} = props;
  const {
    email, username, salutation, firstName, middleName, lastName, suffix, company, title, 
    securityQuestionNo1, securityAnswerNo1, securityQuestionNo2, securityAnswerNo2,
    securityQuestionNo3, securityAnswerNo3, email1, email2, telNo, altTelNo, shippingAddress1, 
    shippingAddress2, shippingAddress3, shippingCity, shippingState, shippingPostalCode, 
    shippingNation, billingAddress1, billingAddress2, billingAddress3, billingCity, 
    billingState, billingPostalCode, billingNation, planChoice, cardName, cardType, cardNumber,
    expiryMM, expiryYYYY, cvv,
  } = user;

Is there a less ghastly way to declare all these variables inside my component?有没有一种不那么可怕的方法来在我的组件中声明所有这些变量? Is there anyway to move this eyesore above into a separate file and import the variables?有没有办法把上面的这个讨厌的东西移到一个单独的文件中并导入变量? Something that's better than changing all subsequent appearances of these variables user.email , user.username , etc. (I thought of using var and moving the declaration to the bottom of the file, but these are not things that should be global variables.)比更改这些变量user.emailuser.username等的所有后续外观更好的东西(我想使用var并将声明移到文件底部,但这些不应该是全局变量。)

To the person who suggested that string editing was the way to go, the answer is NO, because (a) it's slow because converting between JSON.stringify objects and back to objects is slow, (b) it's also insecure (someone can inject a string into you code easily enough, since this code will be running on the front end), and (c) it's terrible form, for which Uncle Bob, should he do JS, would scold you extensively.对于建议字符串编辑是 go 的方法的人来说,答案是否定的,因为(a)它很慢,因为在 JSON.stringify 对象和返回对象之间转换很慢,(b)它也是不安全的(有人可以注入很容易地把字符串插入你的代码,因为这段代码将在前端运行),并且(c)这是一种糟糕的形式,如果 Bob 大叔做 JS,他会大骂你。

No, there is not.不,那里没有。 If you want the variables, you have to declare them.如果你想要变量,你必须声明它们。

Your underlying issue appears to be that your user object has far too many properties.您的根本问题似乎是您的user object 具有太多属性。 Use a composite of many smaller objects instead.改为使用许多较小对象的组合 Put everything with a common prefix in a sub-structure at least.至少将具有公共前缀的所有内容放在子结构中。 Use arrays instead of numbering your properties.使用 arrays 而不是为您的属性编号。 A user doesn't have a cardtype, a user has a card which has a type.一个用户没有卡片类型,一个用户有一张卡片,它有一个类型。

Why don't you use the new optional chaining operator ?.为什么不使用新的可选链接运算符?. . . It short-circuits if the left-hand side is null or undefined and the entire expression yields null.如果左侧是nullundefined ,则它会短路,整个表达式产生 null。 For example, const x = a?.b?.c is roughly equivalent to:例如, const x = a?.b?.c大致相当于:

const x = a === null || a === undefined
        ? undefined
        : a.b === null || a.b === undefined
        ? undefined
        : a.b.c
        ;

Then you can dispense with the destructuring and just say things like然后你可以省去解构,只说类似的话

if ( user?.email ) {
  sendEmail(user);
}

There is away to get around that有办法解决这个问题

 user={ email:"as@gmail.com", username:1, salutation:"hi", firstName:"smith", middleName:"do", lastName:"wi", suffix:"suf", company:"sm", title:"se", securityQuestionNo1:"smth", securityAnswerNo1:"js", securityQuestionNo2:"script", securityAnswerNo2:"node"} Object.entries(user).forEach((item) => { globalThis[item[0]] = item[1] // use window instead of glabalThis if you are not using Nodejs }) console.log(email, username, salutation, firstName, middleName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM