将数据推入对象会清除其他值
Pushing data into object clears the other values
我正在尝试将数据推送到一个对象中,但是一旦我将数据推送到 userID.name
,userID.age
的值就会在控制台中重置(?)。这是我的代码:
if (input.indexOf("ben") >= 0){
var slot = splitInput.indexOf("ben");
console.log(slot)
i = slot + 1;
if (splitInput[i].indexOf(0) >= 0 || splitInput[i].indexOf(1) >= 0 || splitInput[i].indexOf(3) >= 0 || splitInput[i].indexOf(4) >= 0 || splitInput[i].indexOf(4) >= 0 || splitInput[i].indexOf(5) >= 0 || splitInput[i].indexOf(6) >= 0 || splitInput[i].indexOf(7) >= 0 || splitInput[i].indexOf(8) >= 0 || splitInput[i].indexOf(9) >= 0){
i = 0;
var slot = splitInput.indexOf("ben");
// console.log(slot)
i = slot + 1;
userID.age = splitInput[i];
console.log(userID);
} if (splitInput[i].indexOf("a") >= 0 || splitInput[i].indexOf("e") >= 0 || splitInput[i].indexOf("i") >= 0 || splitInput[i].indexOf("u") >= 0){
i = 0;
var slot = splitInput.indexOf("ben");
// console.log(slot)
i = slot + 1;
userID.name = splitInput[i];
console.log(userID);
}
}
这是我的 splitInput
:
var splitInput = input.split(" ");
通过 getElementById
函数收集输入。
当我手动记录 userID
时,我收到此错误 VM935:1 Uncaught ReferenceError: userID is not defined(…)
,这可能与它有关,尽管 console.log(userID)
工作正常。
如果您需要更多信息,请告诉我。
提前致谢!
在分配给对象 属性 之前,如 UserID.age
,您必须首先定义 UserID
本身。
所以在你访问 UserID
的 属性 之前放这个:
var userID = {};
代码备注
用元音检查数字和单词的方式并不是很好。它有很多重复的代码。同样在 if
块内,您再次搜索单词 "ben",而您已经完成了……似乎没有必要再次这样做。
看看这个版本的代码:
// Sample data
var input = 'ik ben 51 jaar';
var splitInput = input.split(" ");
// This was missing:
var userID = {};
// Move retrieval of slot before the `if` so it can be reused
var slot = splitInput.indexOf("ben");
if (slot >= 0){
console.log('slot:', slot);
i = slot + 1;
// Make sure you are not at the end of the array, and
// use a regular expression to see next word consists of digits only
if (i < splitInput.length && splitInput[i].match(/^\d+$/)){
// convert the word to number with unitary plus:
userID.age = +splitInput[i];
}
// You'll maybe want to do an else here.
// Use regular expression again; don't forget the "o"
else if (i < splitInput.length && splitInput[i].match(/a|e|i|o|u/)){
userID.name = splitInput[i];
}
console.log(userID);
}
我正在尝试将数据推送到一个对象中,但是一旦我将数据推送到 userID.name
,userID.age
的值就会在控制台中重置(?)。这是我的代码:
if (input.indexOf("ben") >= 0){
var slot = splitInput.indexOf("ben");
console.log(slot)
i = slot + 1;
if (splitInput[i].indexOf(0) >= 0 || splitInput[i].indexOf(1) >= 0 || splitInput[i].indexOf(3) >= 0 || splitInput[i].indexOf(4) >= 0 || splitInput[i].indexOf(4) >= 0 || splitInput[i].indexOf(5) >= 0 || splitInput[i].indexOf(6) >= 0 || splitInput[i].indexOf(7) >= 0 || splitInput[i].indexOf(8) >= 0 || splitInput[i].indexOf(9) >= 0){
i = 0;
var slot = splitInput.indexOf("ben");
// console.log(slot)
i = slot + 1;
userID.age = splitInput[i];
console.log(userID);
} if (splitInput[i].indexOf("a") >= 0 || splitInput[i].indexOf("e") >= 0 || splitInput[i].indexOf("i") >= 0 || splitInput[i].indexOf("u") >= 0){
i = 0;
var slot = splitInput.indexOf("ben");
// console.log(slot)
i = slot + 1;
userID.name = splitInput[i];
console.log(userID);
}
}
这是我的 splitInput
:
var splitInput = input.split(" ");
通过 getElementById
函数收集输入。
当我手动记录 userID
时,我收到此错误 VM935:1 Uncaught ReferenceError: userID is not defined(…)
,这可能与它有关,尽管 console.log(userID)
工作正常。
如果您需要更多信息,请告诉我。
提前致谢!
在分配给对象 属性 之前,如 UserID.age
,您必须首先定义 UserID
本身。
所以在你访问 UserID
的 属性 之前放这个:
var userID = {};
代码备注
用元音检查数字和单词的方式并不是很好。它有很多重复的代码。同样在 if
块内,您再次搜索单词 "ben",而您已经完成了……似乎没有必要再次这样做。
看看这个版本的代码:
// Sample data
var input = 'ik ben 51 jaar';
var splitInput = input.split(" ");
// This was missing:
var userID = {};
// Move retrieval of slot before the `if` so it can be reused
var slot = splitInput.indexOf("ben");
if (slot >= 0){
console.log('slot:', slot);
i = slot + 1;
// Make sure you are not at the end of the array, and
// use a regular expression to see next word consists of digits only
if (i < splitInput.length && splitInput[i].match(/^\d+$/)){
// convert the word to number with unitary plus:
userID.age = +splitInput[i];
}
// You'll maybe want to do an else here.
// Use regular expression again; don't forget the "o"
else if (i < splitInput.length && splitInput[i].match(/a|e|i|o|u/)){
userID.name = splitInput[i];
}
console.log(userID);
}