简体   繁体   English

我可以创建一个引用对象中节点的变量吗?

[英]Can I create a variable that references a node in an object?

I have this piece of code. 我有这段代码。 It increments a value in an array or sets it to 1 if undefined: 它会增加数组中的值,或者如果未定义则将其设置为1:

if(typeof sum[period][count] === "undefined"){
    sum[period][count] = 1
}else{
    sum[period][count]++;
}

I would like to make it shorter and less repetitive like this: 我想让它更短,更少重复这样:

node = sum[period][count];
if(typeof node === "undefined"){
    node = 1
}else{
    node++;
}

or even 甚至

node = typeof node === "undefined"? 1 : node+1;

But it doesn't work, why not and what can I do about it? 但它不起作用,为什么不起作用,我能做些什么呢?

since the value sum[period][count] is a primitive, you can't get a reference to it. 由于值sum[period][count]是一个原语,因此无法获得对它的引用。 You could shorten it by assigning the object containing this value to a variable: var node = sum[period] and test for node[count] . 您可以通过将包含此值的对象分配给变量来缩短它: var node = sum[period]并测试node[count] However, it is not much shorter: 但是,它不会短得多:

var node = sum[period];
if(typeof node[count] === "undefined"){
    node[count] = 1
}else{
    node[count]++;
}

因为node的值不是Reference

I believe your solution will work in some cases, but in others it will not. 我相信你的解决方案在某些情况下会有效,但在其他情况下它不会。 This is a consequence of using an array of arrays (two-dimensional array/hash) and only checking if the 2nd order value is undefined. 这是使用数组数组(二维数组/散列)并仅检查二阶值是否未定义的结果。

When you define a list of lists (eg 'sum') you don't create a 2D matrix. 定义列表列表(例如“sum”)时,不会创建2D矩阵。 Instead, you are creating a 1D list of 1D lists. 相反,您正在创建一维列表的一维列表。 If the first list index (period) is found, then it will get that list and look for the second index (count) within that 2nd list. 如果找到第一个列表索引(句点),那么它将获得该列表并查找该第二个列表中的第二个索引(计数)。 But if the first list doesn't contain that first index, then you'll get an error immediately since you are referencing an index in an undefined list. 但是如果第一个列表不包含第一个索引,那么由于您在未定义的列表中引用索引,因此您将立即收到错误。

The solution is to check if the first order index value is undefined before checking the second. 解决方案是在检查第二个之前检查第一个订单索引值是否未定义。 For reusability, I'm packaging the code up in a function. 为了重用,我将代码打包在一个函数中。 You will want to remove the JQuery output; 您将要删除JQuery输出; it's just added for my testing. 它刚刚添加到我的测试中。

var sum = {0: {0: 2}};
var period = 0;
var count = 1;

var helper = function(idxa, idxb) {
  if (sum[idxa] === undefined) {
    sum[idxa] = {idxb: 1}; // we can define the new list, knowing idxb must be 1 now.
  } else {
    // you were onto the right code here, but you weren't validating sum[idxa] separately.
    sum[idxa][idxb] = (sum[idxa][idxb] === undefined) ? 1 : sum[idxa][idxb] + 1;
  }
  $('#output').append(" After: " + sum[idxa][idxb]);
};

helper(0, 3); // outputs " After: 1". [0] was defined, but [0][3] wasn't.
helper(2, 3); // outputs " After: 1". Neither [2] nor [2][3] were defined.
helper(0, 0); // outputs " After: 3", because [0][0] was 2
helper(0, 0); // outputs " After: 4", because we already incremented to 3.

You could nest a ternary operation inside a ternary, but I didn't because it's no faster and it wouldn't improve readability. 你可以在三元组中嵌套三元运算,但我没有,因为它不会更快,也不会提高可读性。

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

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