JavaScript程序 - 从数组中删除一个元素 [英] Javascript program - deleting an element from an array

查看:99
本文介绍了JavaScript程序 - 从数组中删除一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的程序是关于运动器材公司如何监督蹦床使用;它记录了客户名称,及其状态(儿童或成人)是目前在蹦床上。有五个功能,让我们可以添加客户,显示客户和删除最后一个客户。我停留在最后一个功能,我不得不使用对象的构造函数来确定,然后删除客户。

PS :我不能用pdefined JavaScript数组元素删除或操作方法,如删除()的$ P $ CONCAT()加入() POP()推()

常量MAX_CUSTOMERS = 5; //蹦床上最大的客户是5VAR customerList =新的Array(); //创建新的磁盘阵列功能addCustomer()
{
     如果(customerList.length> = MAX_CUSTOMERS)
        警报('对不起,没有比'+字符串(MAX_CUSTOMERS)+'允许客户在蹦床上。')
     其他
    {
        VAR newIndex = customerList.length;
        customerList [newIndex] =新对象;
        customerList [newIndex]。名称=提示符(?什么是客户\\'名字'); //要求用户输入他们的姓名
        customerList [newIndex] .STATUS =提示(你是儿童或成人?'); //要求用户输入他们的状态
        而(!(customerList [newIndex] .STATUS =='孩子'|| customerList [newIndex] .STATUS =='成人'))
        {
            customerList [newIndex] .STATUS =(提示('错误请输入\\'孩子\\或\\成人\\':'));
        }    }
}功能displayAllCustomers()
{
    VAR消息='';
    对于(VAR I = 0; I< customerList.length;我++)
    {
        消息+ = customerList [I]。名称+',状态:
                    +字符串(customerList [I] .STATUS)+'。 \\ n';
    }
    如果(消息=='')
        消息='有没有顾客来显示';
    警报(消息);}功能deleteLastCustomer()
{
    如果(customerList.length大于0)
    {
        customerList.length--;
        警报('最后的客户已被删除。');
    }
    其他
        警报(有没有客户删除!');
}
功能identifyThenDeleteCustomer()
{
    VAR客户名称=提示符('输入客户要删除的名字:');
    VAR customerStatus =提示(请输入\\'孩子\\或\\成人\\':');
    而(!(customerStatus =='孩子'|| customerStatus =='成人'))
        customerStatus =提示符('错误 - 输入\\'孩子\\或\\成人\\':');
    deleteCustomer(客户名称,customerStatus);
}功能deleteCustomer(aName,aStatus)
{
    ;
}


解决方案

解决方案

在蹦床

  //最大客户是5
常量MAX_CUSTOMERS = 5;//创建新的磁盘阵列
VAR customerList =新的Array();//添加客户
功能addCustomer(){    //检查最大客户
    如果(customerList.length> = MAX_CUSTOMERS){
        警报('对不起,没有比'+字符串(MAX_CUSTOMERS)+'允许客户在蹦床上。');
    }其他{
        //添加新用户
        VAR newIndex = customerList.length;
        customerList [newIndex] =新对象;        //要求用户输入他们的姓名
        customerList [newIndex]。名称=提示符(?什么是客户\\'名字');        //要求用户输入他们的状态
        customerList [newIndex] .STATUS =提示(你是儿童或成人?');        //检查用户是儿童或者成人
        而(!(customerList [newIndex] .STATUS =='孩子'|| customerList [newIndex] .STATUS =='成人')){
            customerList [newIndex] .STATUS =(
            提示('错误请输入\\'孩子\\或\\成人\\':'));
        }
    }
}//显示客户
功能displayAllCustomers(){
    //创建消息
    VAR消息='';    //循环客户
    对于(VAR I = 0; I< customerList.length;我++){
        //添加客户信息
        消息+ = customerList [I]。名称+',状态:'+字符串(customerList [I] .STATUS)+'。 \\ n';
    }    //检查消息
    如果(消息==''){
        消息='有没有顾客来显示';
    }    //输出消息
    警报(消息);
}//删除最后一个客户
功能deleteLastCustomer(){
    //检查客户名单
    如果(customerList.length大于0){
        //删除最后一个客户
        customerList.length--;
        警报('最后的客户已被删除。');
    }其他{
        警报(有没有客户删除!');
    }
}//找出然后删除客户
功能identifyThenDeleteCustomer(){
    //获取客户名称
    VAR客户名称=提示符('输入客户要删除的名字:');    //获取客户状态
    VAR customerStatus =提示(请输入\\'孩子\\或\\成人\\':');    //检查客户状态
    而(!(customerStatus =='孩子'|| customerStatus =='成人')){
        customerStatus =提示符('错误 - 输入\\'孩子\\或\\成人\\':');
    }    //删除客户
    deleteCustomer(客户名称,customerStatus);
}//删除客户
功能deleteCustomer(aName,aStatus){
    //创建新的数组
    VAR newCustomerList =新的Array();    //循环客户
    对于(VAR I = 0; I< customerList.length;我++){
        VAR的客户= customerList [I]        //检查客户
        如果((customer.name!= aName)||(customer.status!= aStatus)){
            //添加新用户
            VAR newIndex = newCustomerList.length;
            newCustomerList [newIndex] =客户;
        }
    }    //检查删除
    如果(newCustomerList.length< customerList.length){
        警报(客户已被删除。');
    }其他{
        警报(有没有客户删除!');
    }    //更新客户名单
    customerList = newCustomerList;
}

所以,上面可以找到解决方案,因为布兰登提到的,一个新的数组已经建立,其中每​​个客户加入如果这个客户的不是一个你所期待的。因此,留给你一个数组的没有您要找的客户,这个新的数组,那么会将原始之一。

The program I wrote is about how a sports equipment company monitor the trampoline use; it records the customer NAME, and their STATUS (child or adult) that are currently on the trampoline. There are five functions, so we can add customer, display customer and delete the last customer. I am stuck on the last function where I have to use object constructor to identify and then delete the customer.

PS: I can't use any predefined JavaScript array element-deleting or manipulating methods such as delete(), concat(), join(), pop(), push()

const MAX_CUSTOMERS = 5; //maximum customer on the trampoline is 5

var customerList = new Array();//create new Array

function addCustomer () 
{
     if (customerList.length >= MAX_CUSTOMERS)
        alert('Sorry, no more than ' + String(MAX_CUSTOMERS) + ' customers are allowed on the trampoline.')
     else
    {
        var newIndex = customerList.length; 
        customerList[newIndex]  = new Object;
        customerList[newIndex].name = prompt('What is the customer\'s name?'); //ask user enter their name
        customerList[newIndex].status = prompt('Are you a Child or an Adult?'); //ask user enter their status      
        while (!(customerList[newIndex].status == 'child' || customerList[newIndex].status == 'adult'))
        {
            customerList[newIndex].status = (prompt('Error Please Enter \'child\' or \'adult\':'));
        }       

    }
}

function displayAllCustomers () 
{
    var message = '';
    for (var i = 0 ; i < customerList.length ; i++) 
    {
        message += customerList[i].name + ', Status: ' 
                    + String(customerList[i].status) + '. \n'; 
    }
    if (message == '')
        message = 'There are no customer to display!';
    alert(message);

}

function deleteLastCustomer () 
{
    if (customerList.length > 0)
    {
        customerList.length--;
        alert('The last customer has been deleted.');
    }
    else
        alert('There are no customer to delete!');             
}


function identifyThenDeleteCustomer ()
{
    var customerName = prompt('Enter the name of the customer to delete:');
    var customerStatus = prompt('Enter \'child\' or \'adult\':');
    while (!(customerStatus == 'child' || customerStatus == 'adult'))
        customerStatus = prompt('Error - enter \'child\' or \'adult\':');
    deleteCustomer(customerName,customerStatus);    
}

function deleteCustomer (aName, aStatus)
{
    ;               
} 

解决方案

Solution

//maximum customer on the trampoline is 5
const MAX_CUSTOMERS = 5;

//create new Array
var customerList = new Array();

//add customer
function addCustomer() {

    //check max customers
    if (customerList.length >= MAX_CUSTOMERS) {
        alert('Sorry, no more than ' + String(MAX_CUSTOMERS) + ' customers are allowed on the trampoline.');
    } else {
        //add new user
        var newIndex = customerList.length;
        customerList[newIndex] = new Object;

        //ask user enter their name
        customerList[newIndex].name = prompt('What is the customer\'s name?');

        //ask user enter their status   
        customerList[newIndex].status = prompt('Are you a Child or an Adult?');

        //check user is child or adult
        while (!(customerList[newIndex].status == 'child' || customerList[newIndex].status == 'adult')) {
            customerList[newIndex].status = (
            prompt('Error Please Enter \'child\' or \'adult\':'));
        }
    }
}

//display customers
function displayAllCustomers() {
    //create message
    var message = '';

    //loop customers
    for (var i = 0; i < customerList.length; i++) {
        //add customer to message
        message += customerList[i].name + ', Status: ' + String(customerList[i].status) + '. \n';
    }

    //check message
    if (message == '') {
        message = 'There are no customer to display!';
    }

    //output message
    alert(message);
}

//delete last customer
function deleteLastCustomer() {
    //check customer list
    if (customerList.length > 0) {
        //delete last customer
        customerList.length--;
        alert('The last customer has been deleted.');
    } else {
        alert('There are no customer to delete!');
    }
}

//identify then delete customer
function identifyThenDeleteCustomer() {
    //get customer name
    var customerName = prompt('Enter the name of the customer to delete:');

    //get customer status
    var customerStatus = prompt('Enter \'child\' or \'adult\':');

    //check customer status
    while (!(customerStatus == 'child' || customerStatus == 'adult')) {
        customerStatus = prompt('Error - enter \'child\' or \'adult\':');
    }

    //delete customer
    deleteCustomer(customerName, customerStatus);
}

//delete customer
function deleteCustomer(aName, aStatus) {
    //create new array
    var newCustomerList = new Array();

    //loop customers
    for (var i = 0; i < customerList.length; i++) {
        var customer = customerList[i];

        //check customer
        if ((customer.name != aName) || (customer.status != aStatus)) {
            //add new user
            var newIndex = newCustomerList.length;
            newCustomerList[newIndex] = customer;
        }
    }

    //check deleted
    if (newCustomerList.length < customerList.length) {
        alert('The customer has been deleted.');
    } else {
        alert('There are no customer to delete!');
    }

    //update customer list
    customerList = newCustomerList;
}

So above you can find the solution, as Brandon mentioned, a new array has been created, in which each customer was added if this customer was not the one you were looking for. Therefore leaving you with an array without the customer you were looking for, this new array then replaces your original one.

这篇关于JavaScript程序 - 从数组中删除一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆