Javascript程序-从数组中删除元素
我写的程序是关于一家体育器材公司如何监控蹦床的使用;它记录了客户的姓名,以及他们目前在蹦床上的儿童或成人的状态。有五个功能,我们可以添加客户、显示客户和删除最后一个客户。我被最后一个函数卡住了,我必须使用对象构造函数来识别并删除客户 PS:我不能使用任何预定义的JavaScript数组元素删除或操作方法,如delete、concat、join、pop、push const MAX_客户=5//蹦床上的最大顾客人数是5人 var customerList=新数组//创建新阵列 功能添加客户 { 如果customerList.length>=最大客户数 提醒“对不起,蹦床上最多允许有“+StringMAX_客户+”客户。” 其他的 { var newIndex=customerList.length; customerList[newIndex]=新对象; customerList[newIndex].name=提示“客户的名字是什么?”;//要求用户输入他们的名字 customerList[newIndex]。状态=提示“您是儿童还是成人?”;//请用户输入他们的状态 while!customerList[newIndex]。状态=='child'| | customerList[newIndex]。状态=='成人' { customerList[newIndex]。状态=提示“错误,请输入'child\'或'成人\':”; } } } 功能显示所有客户 { var消息=; 对于变量i=0;iJavascript程序-从数组中删除元素,javascript,arrays,Javascript,Arrays,我写的程序是关于一家体育器材公司如何监控蹦床的使用;它记录了客户的姓名,以及他们目前在蹦床上的儿童或成人的状态。有五个功能,我们可以添加客户、显示客户和删除最后一个客户。我被最后一个函数卡住了,我必须使用对象构造函数来识别并删除客户 PS:我不能使用任何预定义的JavaScript数组元素删除或操作方法,如delete、concat、join、pop、push const MAX_客户=5//蹦床上的最大顾客人数是5人 var customerList=新数组//创建新阵列 功能添加客户 { 如
您可以返回一个新数组
var fruits = ['apple', 'banana', 'carrot'], // This is an array of fruits.
deleteFruit = function (name) { // A name of a fruit is an argument.
var i = 0,
newFruits = [];
fruits.forEach(function (fruit) { // It cycles through the list of fruits.
if (fruit !== name) { // If the current fruit is not the argument,
newFruits[i] = fruit; // add the fruit to a new array of fruits.
i++;
}
});
return newFruits; // Return the new array of fruits.
},
printFruits = function () {
fruits.forEach(function (fruit) {
alert(fruit);
});
},
exec = function () {
fruits = deleteFruit('apple'); // Set the old array equal to the returned array.
};
exec();
printFruits();
JSFiddle:
编辑:
增加了澄清意见。这样更好吗?其思想是,您可以通过创建一个新的水果数组、添加所有不是已删除水果的水果并返回该新数组来重新创建拼接方法的功能
var fruits = ['apple', 'banana', 'carrot'], // This is an array of fruits.
deleteFruit = function (name) { // A name of a fruit is an argument.
var i = 0,
newFruits = [];
fruits.forEach(function (fruit) { // It cycles through the list of fruits.
if (fruit !== name) { // If the current fruit is not the argument,
newFruits[i] = fruit; // add the fruit to a new array of fruits.
i++;
}
});
return newFruits; // Return the new array of fruits.
},
printFruits = function () {
fruits.forEach(function (fruit) {
alert(fruit);
});
},
exec = function () {
fruits = deleteFruit('apple'); // Set the old array equal to the returned array.
};
exec();
printFruits();
在这种情况下,我们删除水果“苹果”。因此,我们循环浏览苹果、香蕉、胡萝卜等水果的清单。对于每个水果,如果不是苹果,我们将其添加到新的水果数组中。这意味着我们新的水果系列包含香蕉和胡萝卜。函数返回新的水果数组,并将其分配给旧的水果数组
如果你从三个水果开始,然后以两个水果结束,你就删除了一个。您不必使用拼接。事实上,如果像splice这样的函数以类似的方式执行其功能,我也不会感到惊讶,尽管发明splice的人肯定比我做得更好
我希望这有帮助
另外,胡萝卜现在是水果了 解决方案
如上所述,您可以找到解决方案,正如Brandon提到的,已经创建了一个新的阵列,如果该客户不是您要寻找的客户,则会在其中添加每个客户。因此,如果您的阵列中没有您要寻找的客户,那么这个新阵列将取代您原来的阵列。如果您要删除,为什么要输入状态?为什么不能使用.splice从阵列中删除元素?这是家庭作业吗?如果是这样,请提供准确的说明,以便我们知道实际被问到的问题。是的,这是我们必须为我的一个模块完成的练习。关于第一个问题,我们必须输入status和name,这样它才能匹配我们要删除的元素。也就是说,如果两个人都叫james,一个孩子,一个成人,通过输入状态,我们就知道要删除哪一个。讲座还规定,我们不能使用任何预定义的JavaScript数组元素删除或操纵方法,如delete、concat、join、pop、push splice,这就是我被卡住的原因:我不明白,请详细解释一下