gpt4 book ai didi

javascript - 如何用 2 "steps": `objectVariable[step1][step2] = result;` 初始化对象变量

转载 作者:行者123 更新时间:2023-11-30 14:04:36 25 4
gpt4 key购买 nike

原始问题:

我正在尝试创建一个新对象,但插入有点复杂:

我正在一个对象内部生成一个对象。

result = Object.values(window.datas).reduce( (newObj, dataRow) => {
if ( (user.user_id == dataRow.user_id) && (that.dataResult[dataRow.data_id] !== undefined) && (that.dataResult[dataRow.data_id].length !== 0) ) {
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].name][that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
}
return newObj;
}, {} );

我收到这个错误:

Error in v-on handler: "TypeError: Cannot set property '2017-02-01' of undefined"

线上:

newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].name][that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];

我尝试了什么:

  1. 将此行更改为只有一个键对象(而不是 2 个)时(为不需要的结果,但仅用于测试),它起作用了:

newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].name] = that.dataResult[dataRow.data_id];

  1. 我尝试做这样的事情——添加一个临时变量并将结果推送给它,一开始我以为我解决了问题,但在检查了几次之后我注意到结果重复了并且“cat_id” s 正在重复。

result = Object.values(window.datas).reduce( (newObj, dataRow) => {
if ( (user.user_id == dataRow.user_id) && (that.dataResult[dataRow.data_id] !== undefined) && (that.dataResult[dataRow.data_id].length !== 0) ) {

temp[that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].app_name] = temp;
}
return newObj;

}, {} );
temp = {};

主要问题:

以这种方式设置对象变量的正确方法是什么:

objectVariable[step1][step2] = result; 

完整代码:

let that    = this;
let result = null;
let temp = {};

this.activeUsers.forEach( user => {

result = Object.values(window.datas).reduce( (newObj, dataRow) => {
if ( (user.user_id == dataRow.user_id) && (that.dataResult[dataRow.data_id] !== undefined) && (that.dataResult[dataRow.data_id].length !== 0) ) {
newObj[that.catToApp[that.dataResult[dataRow.data_id].cat_id].app_name][that.dataResult[dataRow.data_id].ts] = that.dataResult[dataRow.data_id];
}
return newObj;
}, {} );


if (Object.entries(result).length !== 0) {
that.usersToDatas[user.user_id] = result;
}

temp = {};
});

最佳答案

ECMAScript 2015 (ES6) 起,您可以使用 computed property names 声明一个对象:

const step1 = 'firstStep', step2 = 'secondStep';

let obj = {
[step1]: {
[step2]: 'value'
}
};

console.log(obj[step1][step2]);

在循环和对象解构中:

const steps = [
['loopA', 'loopA_step1', 'loopA_step2', 'loopA_value'],
['loopB', 'loopB_step1', 'loopB_step2', 'loopB_value']
];

let obj = {
'defaultKeyA': 'defaultValueA', // These pairs will be added in the object each
'defaultKeyB': 'defautlValueB' // time the loop will be done, into "loopX".
};

for (let i = 0; i < steps.length; i++) {
const step = steps[i];

// Creates the key "loopX".
obj[step[0]] = {
// Inserts original "obj" content into the new object.
...obj,

// Creates the key "loopX_step1" with an object as value.
[step[1]]: {
// Creates the key "loopX_step2" with "loopX_value" as value.
[step[2]]: step[3]
}
};
}

console.log(obj);

关于javascript - 如何用 2 "steps": `objectVariable[step1][step2] = result;` 初始化对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55690620/

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