我有一个包含对象的多维数组,如果对象包含特定属性,我希望从数组中删除它们。
咖啡脚本
for dataColumn in allDataColumns
for brentSpiner, i in dataColumn
console.log i, brentSpiner.refreshRate
#dataColumn.remove i if brentSpiner.refreshRate is -1
console.log
当下面的行被注释掉时,上述工作按预期工作
for dataColumn in allDataColumns
for brentSpiner, i in dataColumn
console.log i, brentSpiner.refreshRate
dataColumn.remove i if brentSpiner.refreshRate is -1
上面的错误是这样的:brentSpiner is undefined console.log(i, brentSpiner.refreshRate);
在萤火虫中
第二行的存在究竟如何导致上一行中的变量未定义?
渲染的 JAVASCRIPT
失败
for (_i = 0, _len = allDataColumns.length; _i < _len; _i++) {
dataColumn = allDataColumns[_i];
for (i = _j = 0, _len1 = dataColumn.length; _j < _len1; i = ++_j) {
brentSpiner = dataColumn[i];
console.log(i, brentSpiner.refreshRate);
if (brentSpiner.refreshRate === -1) {
dataColumn.remove(i);
}
}
}
作品
for (_i = 0, _len = allDataColumns.length; _i < _len; _i++) {
dataColumn = allDataColumns[_i];
for (i = _j = 0, _len1 = dataColumn.length; _j < _len1; i = ++_j) {
brentSpiner = dataColumn[i];
console.log(i, brentSpiner.refreshRate);
}
}
(旁注:.remove 已通过 Resig 添加到 Array 原型中)
更新
这是我的一个逻辑错误。查看批准的答案以了解原因。贝娄是我最终做的并且效果很好:
for dataColumn in allDataColumns
i = 0
len = dataColumn.length
while i < len
if dataColumn[i].refreshRate is -1
dataColumn.remove i
len--
i++
渲染
for (_i = 0, _len = allDataColumns.length; _i < _len; _i++) {
dataColumn = allDataColumns[_i];
i = 0;
len = dataColumn.length;
while (i < len) {
if (dataColumn[i].refreshRate === -1) {
dataColumn.remove(i);
len--;
}
i++;
}
}