删除重复的数组元素

时间:2014-11-13 18:07:49

标签: javascript arrays duplicate-removal

我正在开发一个记录客户名称和状态(子/成人)的程序,该程序允许添加,显示和删除数组中的客户记录。但是,如果用户输入相同的名称和状态,例如:

姓名:詹姆斯,身份:成人 姓名:詹姆斯,身份:成人

我希望该函数只删除一条记录,但现在删除它们,我必须在这里添加break吗?请帮忙。

PS:我无法使用任何内置的JavaScript函数,例如slice()delete()concat()join()pop(),{{1} },push()reverse()shift()slice()sort()splice()toString()unshift() < / p>

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
   }

1 个答案:

答案 0 :(得分:1)

您可以像这样制作删除功能

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!');
}

它只删除一个。

因为你说你不能使用内置函数。在这种情况下,您必须复制要删除的元素之前和之后的元素。您可以使用控件变量标记您已找到要删除的控件。所以不会再发生删除。

例如,

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
}