我试图找出数组中是否存在值。下面的代码每次运行时都会给我一个错误,表示Object没有替换方法。在Javascript中查找数组中的值
var fruits = ['apples', 'pears', 'bananas'];
console.log("Enter in a fruit name");
process.stdin.on('data', function(fruit) {
fruit = fruit.replace("\n", "");
if (fruits.indexOf(fruit) >= 0) {
console.log("The value has been found in the array");
process.exit(); }
else {
console.log("Value not found");
process.exit(); }
});
起初,它保持返回无论我输入了什么“找不到值”,所以我推测这是换行/输入进入我的水果后,我按。但是水果的替代方法拒绝采用。我错过了什么?
什么是'水果'? – 0x499602D2 2013-04-23 23:03:37
尝试'console.log(JSON.stringify(水果))'看看你在寻找什么。 – Bergi 2013-04-23 23:04:49