gpt4 book ai didi

javascript - 如何添加一个值并嵌套一个对象

转载 作者:行者123 更新时间:2023-11-30 13:54:31 25 4
gpt4 key购买 nike

<分区>

const arr = [
[1, "string1", 'string11'],
[2, "string2", 'string22'],
[3, "string3", 'string33'],
[4, "string4", 'string44'],
];
const obj = arr.reduce((acc, val) => {
for (let i = 0; i < val.length; i += 3) {
acc[val[i]] = {
val: val[i + 2]
}
}
return acc;
}, {});
console.log(obj);

output is :
1: Object
val: "string11"
2: Object
val: "string22"
3: Object
val: "string33"
4: Object
val: "string44"

我想将 val: val[i+2] 替换为 val[i+1]: val[i+2] 使对象成为

1: Object
"string1": "string11"
2: Object
"string2": "string22"
3: Object
"string3": "string33"
4: Object
"string4": "string44"

但是这个语法是不正确的 val[i+1]: val[i+2],有人能告诉我如何让 val 是动态的吗?

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