简体   繁体   English

如何使用键中的特殊字符在 React 中解构 object

[英]How to destructure object in React with special characters in key

Backend returns an object, and one of the key-value, I would handle differently, how can I split up object?后端返回一个 object 和一个键值,我会以不同的方式处理,我该如何拆分 object?

useEffect(() => {
    const tempUserShortId = getTempUserShortId()
    axios({
        method: 'get',
        url: `questionList?tempUserId=${tempUserShortId}&questionIds[]=6f50dbea-196b-4fb6-82ee-fbade62aab98&questionIds[]=6f50dbea-196b-4fb6-82ee-fbade62aab99&questionIds[]=6f50dbea-196b-4fb6-82ee-fbade62aab9a`,
        headers: { crossDomain: true },
    }).then((res) => {

        const {'6f50dbea-196b-4fb6-82ee-fbade62aab9a', ...rest} = res.data;
        setQuestions(rest)
    })
}, [ind])

It says: ':' expected.ts(1005它说: ':' expected.ts(1005

Found this online:在网上找到了这个:

  const removeKey = () => {
    setEmployee(current => {
      // 👇️ remove salary key from object
      const {salary, ...rest} = current;

      return rest;
    });
  };

https://bobbyhadz.com/blog/react-remove-key-from-state-object https://bobbyhadz.com/blog/react-remove-key-from-state-object

It does not work.这没用。

Got back from backend a structure like this:从后端返回一个这样的结构:

{
    "6F50DBEA-196B-4FB6-82EE-FBADE62AAB99": {
        "text": "Mennyire forradalmi az ETH 2.0 bevezetése?",
        "options": {
            "1CD0D2C0-7494-4BE6-ADE6-E454816B584E": {
                "text": "Forradalmi mert PoW helyett PoS lesz. Kevesebb energiát fog fogyasztani a rendszer. Viszont ez még mindig nem skálázható megoldás, a shardingot még nem tudja, és még hosszú évekig nem is fogja tudni, így a tranzakciós díj ugyanúgy magas fog maradni.",
                "rating": {}
            }
        }
    },
    "6F50DBEA-196B-4FB6-82EE-FBADE62AAB98": {

You have to focus the key of your object and set it to "_" to tell that you will not use it你必须把你的 object 的键放在焦点上并将它设置为“_”来告诉你不会使用它

const obj = {
    "6f50dbea-196b-4fb6-82ee-fbade62aab9a": "cool",
    name: "test",
    value: 3
}
const {"6f50dbea-196b-4fb6-82ee-fbade62aab9a": _, ...rest} = obj // rest equal {name: "test", value: 3}

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

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