删除带有“约束”的数组项

时间:2018-02-18 19:59:54

标签: javascript

我正在尝试创建一个删除客户及其地址的例程,但前提是该地址未附加到客户对象列表中的其他客户。

这是我的代码:

//我的数据

var allData = [
   { type: "store", data: { store_id: 297, name: "Scotiabank - Main Branch", 
address_id: 1023 } },
   { type: "store", data: { store_id: 614, name: "Scotiabank - Hamilton", 
address_id: 1984 } },
   { type: "store", data: { store_id: 193, name: "Scotiabank - Mississauga", 
address_id: 1757 } },
   { type: "customer", data: { customer_id: 26, store_id: 297, first_name: 
"Dave", last_name: "Bennett", email: "dbennett@gmail.com", address_id: 4536, 
add_date: null } },
   { type: "customer", data: { customer_id: 59, store_id: 193, first_name: 
"John", last_name: "Stevens", email: "jstevens22@hotmail.com", address_id: 
2473, add_date: null } },
   { type: "customer", data: { customer_id: 29, store_id: 614, first_name: 
"Sarah", last_name: "Pym", email: "spym99@hotmail.com", address_id: 1611, 
add_date: null } },
   { type: "customer", data: { customer_id: 63, store_id: 297, first_name: 
"Steven", last_name: "Edwards", email: "steven2231@hotmail.com", address_id: 
1836, add_date: null } },
   { type: "customer", data: { customer_id: 71, store_id: 614, first_name: 
"Martin", last_name: "Scott", email: "mdog33@gmail.com", address_id: 2727, 
add_date: null } },
   { type: "customer", data: { customer_id: 24, store_id: 614, first_name: 
"Jonathan", last_name: "Pym", email: "jjpym@yahoo.ca", address_id: 1611, 
add_date: null } },
   { type: "customer", data: { customer_id: 36, store_id: 193, first_name: 
"Kaitlyn", last_name: "Adams", email: "katy38@hotmail.com", address_id: 
5464, add_date: null } },
   { type: "customer", data: { customer_id: 73, store_id: 297, first_name: 
"Melissa", last_name: "Bennett", email: "mbennett@gmail.com", address_id: 
4536, add_date: null } },
   { type: "address", data: { address_id: 1023, address: "2895 Yonge St.", 
city: "Toronto", province: "ON", postal_code: "L4C02G" } },
   { type: "address", data: { address_id: 1984, address: "3611 Main St. 
West", city: "Hamilton", province: "ON", postal_code: "R5O8H5" } },
   { type: "address", data: { address_id: 1757, address: "1177 Ontario St. 
Unit 8", city: "Mississauga", province: "ON", postal_code: "L9H6B3" } },
   { type: "address", data: { address_id: 4536, address: "3945 John St.", 
city: "Ajax", province: "ON", postal_code: "L7M4T9" } },
   { type: "address", data: { address_id: 2473, address: "391 Baker St. Apt 
231", city: "Mississauga", province: "ON", postal_code: "M4T8S3" } },
   { type: "address", data: { address_id: 1611, address: "183 City Ct.", 
city: "Hamilton", province: "ON", postal_code: "J3T9V2" } },
   { type: "address", data: { address_id: 1836, address: "67 Rhymer Ave.", 
city: "Stouffville", province: "ON", postal_code: "L3C8H4" } },
   { type: "address", data: { address_id: 2727, address: "287 Brant St. Apt 
4A", city: "Waterdown", province: "ON", postal_code: "R93G3P" } },
   { type: "address", data: { address_id: 5464, address: "11 New St. Apt 
2B", city: "Brampton", province: "ON", postal_code: "L694R7" } },
];

//我的对象

var CustomerDB = {
   customers: [],
   addresses: [],
   stores: [],
   insertData: function (allData) {
      for (var i = 0; i < allData.length; i++) {
         if (allData[i].type == "store") {
            this.addStore(allData[i].data);
         }
         else if (allData[i].type == "customer") {
            this.addCustomer((allData[i].data));
         }
         else if (allData[i].type == "address") {
            this.addAddress((allData[i].data));
         }
      }

   },

//加载数据

CustomerDB.insertData(allData);

此removeCustomerById函数完全有效,但removeAddressById:

除外
removeCustomerById: function (customer_id) {

      var address_id_remove;

      for (var i = 0; i < this.customers.length; i++) {
         if (this.customers[i].customer_id == customer_id) {
            address_id_remove = this.customers[i].address_id;
            this.customers.splice(i, 1);
            this.removeAddressById(address_id_remove);
          }

      }

   },

// removeAddresById code

 removeAddressById: function (address_id) {

      for (var i = 0; i < this.addresses.length; i++) 
      {

         if (this.addresses[i].address_id == address_id) 
         {

            if (this.addressExistsInCustomer(address_id))
            {
               console.log("bout to remove an address " + 
this.addresses[i].address_id)
               this.addresses.splice(i, 1);
            }
         }
      }

   },

// addressExistsInCustomer code

    addressExistsInCustomer: function (address_id) {
        return this.customers.some(c => c.address_id == address_id)
   },

addressExistsInCustomer永远不会返回true,即使它应该返回true。我觉得我忽略了一些愚蠢的东西,需要一双新鲜的眼睛......

先谢谢

2 个答案:

答案 0 :(得分:0)

for (var i = 0; i < this.customers.length; i++) {
  return (this.customers[i].address_id == address_id)
}

此循环使函数始终在循环的第一次迭代中返回,无论值是true还是false

您想要做的是:

for (var i = 0; i < this.customers.length; i++) {
  if (this.customers[i].address_id == address_id) return true
}
return false

这确保该函数仅在表达式求值为true时(或 )时返回,并且如果求值为false,则将继续下一次迭代。如果表达式在任何迭代中都没有计算到true,则循环之后的代码将被执行,函数返回false

答案 1 :(得分:0)

您的方法在第一次迭代时返回。

for (var i = 0; i < this.customers.length; i++) {
    return (this.customers[i].address_id == address_id)
}   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

一个好方法是使用函数some

addressExistsInCustomer: function (address_id) {
    return this.customers.some(c => c.address_id == address_id)
}

此片段说明了函数some的工作原理,基本上找到了address_id等于传递为param的对象。

&#13;
&#13;
var customers = [  {    "type": "store",    "data": {      "store_id": 297,      "name": "Scotiabank - Main Branch",      "address_id": 1023    }  },  {    "type": "store",    "data": {      "store_id": 614,      "name": "Scotiabank - Hamilton",      "address_id": 1984    }  },  {    "type": "store",    "data": {      "store_id": 193,      "name": "Scotiabank - Mississauga",      "address_id": 1757    }  },  {    "type": "customer",    "data": {      "customer_id": 26,      "store_id": 297,      "first_name": "Dave",      "last_name": "Bennett",      "email": "dbennett@gmail.com",      "address_id": 4536,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 59,      "store_id": 193,      "first_name": "John",      "last_name": "Stevens",      "email": "jstevens22@hotmail.com",      "address_id": 2473,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 29,      "store_id": 614,      "first_name": "Sarah",      "last_name": "Pym",      "email": "spym99@hotmail.com",      "address_id": 1611,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 63,      "store_id": 297,      "first_name": "Steven",      "last_name": "Edwards",      "email": "steven2231@hotmail.com",      "address_id": 1836,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 71,      "store_id": 614,      "first_name": "Martin",      "last_name": "Scott",      "email": "mdog33@gmail.com",      "address_id": 2727,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 24,      "store_id": 614,      "first_name": "Jonathan",      "last_name": "Pym",      "email": "jjpym@yahoo.ca",      "address_id": 1611,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 36,      "store_id": 193,      "first_name": "Kaitlyn",      "last_name": "Adams",      "email": "katy38@hotmail.com",      "address_id": 5464,      "add_date": null    }  },  {    "type": "customer",    "data": {      "customer_id": 73,      "store_id": 297,      "first_name": "Melissa",      "last_name": "Bennett",      "email": "mbennett@gmail.com",      "address_id": 4536,      "add_date": null    }  },  {    "type": "address",    "data": {      "address_id": 1023,      "address": "2895 Yonge St.",      "city": "Toronto",      "province": "ON",      "postal_code": "L4C02G"    }  },  {    "type": "address",    "data": {      "address_id": 1984,      "address": "3611 Main St. West",      "city": "Hamilton",      "province": "ON",      "postal_code": "R5O8H5"    }  },  {    "type": "address",    "data": {      "address_id": 1757,      "address": "1177 Ontario St. Unit 8",      "city": "Mississauga",      "province": "ON",      "postal_code": "L9H6B3"    }  },  {    "type": "address",    "data": {      "address_id": 4536,      "address": "3945 John St.",      "city": "Ajax",      "province": "ON",      "postal_code": "L7M4T9"    }  },  {    "type": "address",    "data": {      "address_id": 2473,      "address": "391 Baker St. Apt 231",      "city": "Mississauga",      "province": "ON",      "postal_code": "M4T8S3"    }  },  {    "type": "address",    "data": {      "address_id": 1611,      "address": "183 City Ct.",      "city": "Hamilton",      "province": "ON",      "postal_code": "J3T9V2"    }  },  {    "type": "address",    "data": {      "address_id": 1836,      "address": "67 Rhymer Ave.",      "city": "Stouffville",      "province": "ON",      "postal_code": "L3C8H4"    }  },  {    "type": "address",    "data": {      "address_id": 2727,      "address": "287 Brant St. Apt 4A",      "city": "Waterdown",      "province": "ON",      "postal_code": "R93G3P"    }  },  {    "type": "address",    "data": {      "address_id": 5464,      "address": "11 New St. Apt 2B",      "city": "Brampton",      "province": "ON",      "postal_code": "L694R7"    }  }];

var addressExistsInCustomer = (address_id) => {
  return this.customers.some(c => c.data.address_id === address_id);
}

console.log(addressExistsInCustomer(1023));
console.log(addressExistsInCustomer(999900));
&#13;
&#13;
&#13;

资源