简体   繁体   中英

Adding elements from one array to another

I want to store all the Data into a new array depending on its type. If it is "store", the data should be stored in the stores[] array, if the type is customer, the data should be stored in the customer array etc. I'm pretty new to Javascript so I'm not sure if I'm storing it correctly.

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:"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:"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"}},
];


var CustomerDB = 
{
    customer:[],
    addresses:[],
    stores:[],

    add:function insertData (allData)
    {
      for (var i = 0; i < allData.length; i++)
          {
              if (type =="store")
                  {
                      stores = allData.slice(i);

                  }
              else if (type == "customer")
                  {
                      customer = allData.slice(i);
                  }
              else if (type == "address")
                  {
                      addresses = allDara.slice(i);
                  }
           }
    }
}
var CustomerDB = {
  customer: [],    // should be exactly like the type (to make it easier to add)
  address: [],     // this too (should be adress not adresses)
  store: [],       // ...

  add: function insertData(allData) {
    allData.forEach(function(d) {
      this[d.type].push(d);
    });
  }
}

forEach will loop through each object in allData array. For each item, this[d.type] will be evaluated to this["customer"] if, for example, the type is customer which is exactly this.customer (the array for customers).

You can try to run your function outside of customerDB:

var CustomerDB = 
{
  customer:[],
  addresses:[],
  stores:[],
}

var insertData = function(){
  for (var i = 0; i < allData.length; i++)
    //console.log(allData[i]);
      {
          if (allData[i]['type'] == "store")
              {
                  CustomerDB.stores.push(allData[i]);

              }
          else if (allData[i]['type'] == "customer")
              {
                  CustomerDB.customer.push(allData[i]);
              }
          else if (allData[i]['type'] == "address")
              {
                  CustomerDB.addresses.push(allData[i]);
              }
       }
}(); 

Here a codepen: http://codepen.io/giannidk/pen/egbmRL?editors=0012

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM