gpt4 book ai didi

javascript - 使用解构的动态命名值

转载 作者:行者123 更新时间:2023-12-02 21:15:55 25 4
gpt4 key购买 nike

我正在尝试使用解构,但似乎我做错了分配。

这样可以吗?

  let fields = ["FASE", "BAWTOM_1"]
let object = {}
const d = await getData();

const count = d.reduce((acc, elem) => {

({ [fields[0]], [fields[1]] } = elem)
return {...acc, [ fields[1] ]: [...(acc[ fields[1] ] || []), {fase: fields[0] }]};
}, {});

我堆栈在 ({ [fields[0]], [fields[1]] } = elem) 上,我无法让它工作

非常感谢任何帮助

elem 就是这样的东西,还有更多的项目

var elem = [
{"FASE": "2",
"BAWTOM_1": "1",
"BA_SP_1": "0",
"BA_SP_2": "0",
"BA_SP_3": "0",
},
{"FASE": "1",
"BAWTOM_1": "2",
"BA_SP_1": "1",
"BA_SP_2": "0",
"BA_SP_3": "0",
},
{"FASE": "1",
"BAWTOM_1": "1",
"BA_SP_1": "0",
"BA_SP_2": "1",
"BA_SP_3": "0",
},
{"FASE": "2",
"BAWTOM_1": "4",
"BA_SP_1": "1",
"BA_SP_2": "0",
"BA_SP_3": "1",
}
]

最佳答案

好的,明白了。

这是我解决这个问题的方法。当然,这不是生产代码,我只是在玩弄,但现在我可以将其转换为更可用的东西

    async function pippo() {
let fields = ["FASE", "BAWTOM_1"]
let object = {}
const d = await getData();


const count = d.reduce((acc, elem) => {

({[fields[0]]: object[fields[0]], [fields[1]]: object[fields[1]]} = elem);
return {...acc, [ object[fields[1]] ]: [...(acc[ object[fields[1]] ] || []), {fase: object[fields[0]] }]};
}, {});

console.log(count)
}

关于javascript - 使用解构的动态命名值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60973037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com