为什么多维数组只在每个单元格上执行函数?
Why multidimensional array only execute function on every cell?
问题
我有一个 Multidimensional Array
和一个名为 conversion()
的函数。
let variables outside conversion()
只会在其他每个单元格上执行
let variables inside conversion()
工作正常,但你必须在每个新函数中键入完全相同的变量
我在寻找什么?
我想知道为什么会这样
如果我知道为什么,那么我就可以解决问题
但目前我不知道为什么
作品……有点
仅在每隔一个单元格上执行函数
let MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
],
regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/g,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/g,
regBoolean = /^(\s+)?(true|false)(\s+)?$/gi,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/gi,
regNaN = /^(\s)?nan(\s)?$/gi;
function conversion(str) {
if (regNumber.test(str)) {return parseInt(str);}
if (regBoolean.test(str)) {return /t/i.test(str)?true:false;}
if (regUndefined.test(str)) {return /d/i.test(str)?undefined:null;}
if (regNaN.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
let cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
let cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA)
工作正常
效率低下,因为我必须将变量
写入我希望在每个单元格上执行的每个函数。
let MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
];
function conversion(str) {
let regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/g,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/g,
regBoolean = /^(\s+)?(true|false)(\s+)?$/gi,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/gi,
regNaN = /^(\s)?nan(\s)?$/gi;
if (regNumber.test(str)) {return parseInt(str);}
if (regBoolean.test(str)) {return /t/i.test(str)?true:false;}
if (regUndefined.test(str)) {return /d/i.test(str)?undefined:null;}
if (regNaN.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
let cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
let cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA)
我试过的
将 let
变量放在 conversion()
之外
创建了引用外部变量
的新 let
个变量
但它只适用于所有其他单元格
let MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
],
regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/g,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/g,
regBoolean = /^(\s+)?(true|false)(\s+)?$/gi,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/gi,
regNaN = /^(\s)?nan(\s)?$/gi;
function conversion(str) {
let r1 = regComma,
r2 = regNumber,
r3 = regBoolean,
r4 = regUndefined,
r5 = regNaN;
if (r2.test(str)) {return parseInt(str);}
if (r3.test(str)) {return /t/i.test(str)?true:false;}
if (r4.test(str)) {return /d/i.test(str)?undefined:null;}
if (r5.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
let cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
let cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA)
test()
called multiple times on the same global regular expression instance will advance past the previous match.
If the regex has the global flag set, test()
will advance the lastIndex
of the regex. A subsequent use of test()
will start the search at the substring
of str
specified by lastIndex (exec()
will also advance the lastIndex
property). It is worth noting that the lastIndex
will not reset when testing a different string.
因此导致了这种行为。从正则表达式中删除 g
会产生预期的行为。
const MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
];
const regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/,
regBoolean = /^(\s+)?(true|false)(\s+)?$/i,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/i,
regNaN = /^(\s)?nan(\s)?$/i;
function conversion(str) {
if (regNumber.test(str)) {return parseInt(str);}
if (regBoolean.test(str)) {return /t/i.test(str)?true:false;}
if (regUndefined.test(str)) {return /d/i.test(str)?undefined:null;}
if (regNaN.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
const cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
const cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA);
好问题。谢谢。
问题
我有一个 Multidimensional Array
和一个名为 conversion()
的函数。
let variables outside conversion()
只会在其他每个单元格上执行
let variables inside conversion()
工作正常,但你必须在每个新函数中键入完全相同的变量
我在寻找什么?
我想知道为什么会这样
如果我知道为什么,那么我就可以解决问题
但目前我不知道为什么
作品……有点
仅在每隔一个单元格上执行函数
let MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
],
regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/g,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/g,
regBoolean = /^(\s+)?(true|false)(\s+)?$/gi,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/gi,
regNaN = /^(\s)?nan(\s)?$/gi;
function conversion(str) {
if (regNumber.test(str)) {return parseInt(str);}
if (regBoolean.test(str)) {return /t/i.test(str)?true:false;}
if (regUndefined.test(str)) {return /d/i.test(str)?undefined:null;}
if (regNaN.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
let cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
let cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA)
工作正常
效率低下,因为我必须将变量
写入我希望在每个单元格上执行的每个函数。
let MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
];
function conversion(str) {
let regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/g,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/g,
regBoolean = /^(\s+)?(true|false)(\s+)?$/gi,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/gi,
regNaN = /^(\s)?nan(\s)?$/gi;
if (regNumber.test(str)) {return parseInt(str);}
if (regBoolean.test(str)) {return /t/i.test(str)?true:false;}
if (regUndefined.test(str)) {return /d/i.test(str)?undefined:null;}
if (regNaN.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
let cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
let cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA)
我试过的
将 let
变量放在 conversion()
之外
创建了引用外部变量
的新 let
个变量
但它只适用于所有其他单元格
let MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
],
regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/g,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/g,
regBoolean = /^(\s+)?(true|false)(\s+)?$/gi,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/gi,
regNaN = /^(\s)?nan(\s)?$/gi;
function conversion(str) {
let r1 = regComma,
r2 = regNumber,
r3 = regBoolean,
r4 = regUndefined,
r5 = regNaN;
if (r2.test(str)) {return parseInt(str);}
if (r3.test(str)) {return /t/i.test(str)?true:false;}
if (r4.test(str)) {return /d/i.test(str)?undefined:null;}
if (r5.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
let cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
let cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA)
test()
called multiple times on the same global regular expression instance will advance past the previous match.If the regex has the global flag set,
test()
will advance thelastIndex
of the regex. A subsequent use oftest()
will start the search at thesubstring
ofstr
specified by lastIndex (exec()
will also advance thelastIndex
property). It is worth noting that thelastIndex
will not reset when testing a different string.
因此导致了这种行为。从正则表达式中删除 g
会产生预期的行为。
const MDA = [
[ '','','UndefineD','nuLL','trUe','fAlsE','TruE','FalSe','NaN','NAN'],
[ '1', '2', '3', '4', '5', 'TRUE', 'FALSE', '8', '9', '10' ],
[ 'a', 'TrUe', 'FaLsE', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ]
];
const regComma = /\b(,")(.*?(?:(?!\b,".*",).)*)(",)/,
regNumber = /^((\s+)?((-)|\.)?)?[\d\.]+(\s+)?$/,
regBoolean = /^(\s+)?(true|false)(\s+)?$/i,
regUndefined = /^(\s+)?(undefined|null)(\s+)?$/i,
regNaN = /^(\s)?nan(\s)?$/i;
function conversion(str) {
if (regNumber.test(str)) {return parseInt(str);}
if (regBoolean.test(str)) {return /t/i.test(str)?true:false;}
if (regUndefined.test(str)) {return /d/i.test(str)?undefined:null;}
if (regNaN.test(str)) {return NaN;}
return str;
}
for(var i = 0; i < MDA.length; i++) {
const cube = MDA[i];
for(var j = 0; j < cube.length; j++) {
const cell = cube[j],
convert = conversion(cell);
MDA[i][j] = convert;
}
}
console.log(MDA);
好问题。谢谢。