删除重复的数组元素 [英] Delete duplicate array element

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

问题描述

我就可以记录客户名称和状态(儿童/成人)的程序工作,该计划允许从阵列添加,显示和删除客户记录。然而,如果用户输入了相同的名称和状态例如:

名称:詹姆斯,状态:成人
名称:詹姆斯,状态:成人

我要的功能删除一条记录,但现在它删除他们两个,我一定要在这里休息补充?请帮忙。

PS:我不能使用任何内置的JavaScript函数,如片()删除() CONCAT()加入() POP()推()反向()移()片()排序()拼接()的toString()的unshift()的valueOf()

\r
\r

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

\r

<!DOCTYPE HTML>\r
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtmlLANG =ENXML:LANG =ENGT&;\r
\r
< HEAD>\r
<间的charset =UTF-8/>\r
<标题>习作2'; /标题>\r
<脚本SRC =ZouYuncongINSTG018cw2.js类型=文/ JavaScript的>< / SCRIPT>\r
< /头>\r
\r
<身体GT;\r
< D​​IV>\r
<按钮式=按钮的onclick =addCustomer();>添加客户< /按钮>< BR>\r
<按钮式=按钮的onclick =displayAllCustomers();>显示所有的客户与LT; /按钮>< BR>\r
<按钮式=按钮的onclick =identifyThenDeleteCustomer();>确定然后删除客户< /按钮>\r
< / DIV>\r
< /身体GT;\r
\r
< / HTML>

\r

\r
\r


解决方案

您可以让你删除功能这样的,

 函数deleteCustomer(aName,aStatus)//删除客户
{
   对于(VAR I = 0; I< customerList.length;我++)//循环客户
   {
      VAR的客户= customerList [I]
      如果((customer.name == aName)及及(customer.status == aStatus))//检查顾客
      {
         customerList =方法Array.splice(I,1); //从阵列本身删除
         警报(客户已被删除。');
         返回; //停止
      }
   }
   警报(有没有客户删除!');
}

这将删除只有一个。

既然你说你内置函数不能使用。在这种情况下,你必须前后之一删除的元素复制。你可以有一个控制变量标志着你​​已经找到了一个删除。所以没有更多的缺失会发生。

例如,

 函数deleteCustomer(aName,aStatus)//删除客户
{
   VAR onedeleted = FALSE;
   VAR newCustomerList =新的Array(); //创建新的数组
   对于(VAR I = 0; I< customerList.length;我++)//循环客户
   {
      VAR的客户= customerList [I]
      如果((customer.name!= aName)||(customer.status!= aStatus)|| onedeleted)//检查顾客
      {
         VAR newIndex = newCustomerList.length; //添加新用户
         newCustomerList [newIndex] =客户;
      }
      其他
          onedeleted = TRUE;
   }   如果(newCustomerList.length< customerList.length)//检查删除
   {
      警报(客户已被删除。');
   }
   其他
   {
      警报(有没有客户删除!');
   }   customerList = newCustomerList; //更新客户名单
}

I am working on a program that records customer name and status(child/adult), the program allows add, display and delete customer records from array. However, if user enters the same name and status e.g:

Name: james, status: adult Name: james, status: adult

I want the function to delete just one record,but now it delete both of them, do i have to add break here? Please help.

PS: I can't use any inbuilt JavaScript functions such as slice(),delete(), concat(), join(), pop(), push(), reverse(), shift(), slice(), sort(), splice(), toString(), unshift() or valueOf()

const MAX_CUSTOMERS = 5;

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

    function addCustomer() //add customer
       {
          if (customerList.length >= MAX_CUSTOMERS) //check max customers
             alert('Sorry, no more than ' + String(MAX_CUSTOMERS) + ' customers are allowed on the trampoline.')
          else
          {
             var newIndex = customerList.length; //add new user
             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')) //check user is child or adult
             {
                customerList[newIndex].status = (prompt('Error! Please Enter \'child\' or \'adult\':'));
             }
          }
       }

    function displayAllCustomers() //display customers
       {
          var message = ''; //create message
          for (var i = 0; i < customerList.length; i++) //loop customers
          {
             message += 'Name:' + customerList[i].name + ', Status: ' + String(customerList[i].status) + '. \n'; //add customer to message
          }
          if (message == '') //check message
             message = 'Sorry, there are no customer to display!';
          alert(message); //output message

       }


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


function deleteCustomer(aName, aStatus) //delete customer
   {
      var newCustomerList = new Array(); //create new array
      for (var i = 0; i < customerList.length; i++) //loop customers
      {
         var customer = customerList[i];
         if ((customer.name != aName) || (customer.status != aStatus)) //check customer
         {
            var newIndex = newCustomerList.length; //add new user
            newCustomerList[newIndex] = customer;
         }
      }

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

      customerList = newCustomerList; //update customer list
   }

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<meta charset="utf-8" />
<title>Coursework 2</title>
<script src="ZouYuncongINSTG018cw2.js" type="text/javascript"></script>
</head>

<body>
<div>
<button type="button" onclick="addCustomer();">Add Customer</button><br>
<button type="button" onclick="displayAllCustomers();">Display All Customers</button><br>
<button type="button" onclick="identifyThenDeleteCustomer();">Identify then Delete Customer</button>
</div>
</body>

</html>

解决方案

You can make your delete function like this,

function deleteCustomer(aName, aStatus) //delete customer
{
   for (var i = 0; i < customerList.length; i++) //loop customers
   {
      var customer = customerList[i];
      if ((customer.name == aName) && (customer.status == aStatus)) //check customer
      {
         customerList = array.splice(i, 1);//delete from array itself
         alert('The customer has been deleted.');
         return;//stop
      }
   }
   alert('There are no customer to delete!');
}

It will delete just one.

Since you said you cant use built in functions. In that case you have to copy the elements before and after the one to remove. You can have a control variable marking that you already found the one to delete. So no more deletions will happen.

For example,

function deleteCustomer(aName, aStatus) //delete customer
{
   var onedeleted = false;
   var newCustomerList = new Array(); //create new array
   for (var i = 0; i < customerList.length; i++) //loop customers
   {
      var customer = customerList[i];
      if ((customer.name != aName) || (customer.status != aStatus) || onedeleted) //check customer
      {
         var newIndex = newCustomerList.length; //add new user
         newCustomerList[newIndex] = customer;
      }
      else 
          onedeleted = true;
   }

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

   customerList = newCustomerList; //update customer list
}

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

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