如何使用2个“步骤”初始化对象变量:`objectVariable [step1] [step2] =结果;`

时间:2019-04-15 13:47:21

标签: javascript variables ecmascript-6 javascript-objects reduce

原始问题:

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

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

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;
}, {} );

我收到此错误:

  

v-on处理程序中的错误:“ TypeError:无法设置未定义的属性'2017-02-01'”

在线:

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. 我尝试做这样的事情-添加一个temp变量并将其推入结果,一开始我以为我已经解决了问题,但是在查看了几次之后,我注意到结果正在重复并且显示“ 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 = {};
});

4 个答案:

答案 0 :(得分:2)

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);

答案 1 :(得分:0)

您可以使用hasOwnProperty函数来查看对象是否具有属性。

例如

let myObject = {};
let prop = "myProperty";
let prop 2 = "mySecondProperty"

if(!myObject.hasOwnProperty(prop)){
   myObject[prop] = {};
}

myObject[prop][prop2] = ....

如果未定义“ myObject [prop]”,则可以在循环中使用它并设置“默认”值

答案 2 :(得分:0)

您可以进行(objectVariable[step1] = objectVariable[step1] || {})

var objectVariable = {};
var step1 = 'a';
var step2 = 'b';

var result = 'hello world';

(objectVariable[step1] = objectVariable[step1] || {})[step2] = result;

console.log(objectVariable)

答案 3 :(得分:0)

关于objectVariable[step1][step2] = result,您可以使用Object.assign加上计算出的属性,例如:

const myObj = { someProp: "someValue" };
const foo = "propertyFoo";
const bar = "propertyBar";
Object.assign(myObj, { [foo]: { [bar]: "baz" } });
console.log(myObj);
(不过,一般来说,切线上的

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

只是吓人。)