user7218356
user7218356

Reputation: 5

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: "[email protected]", address_id: 4536, add_date: null}},
{type:"customer", data:{customer_id: 59, store_id:193, first_name: "John", last_name: "Stevens", email: "[email protected]", address_id: 2473, add_date: null}},
{type:"customer", data:{customer_id: 29, store_id:614, first_name: "Sarah", last_name: "Pym", email: "[email protected]", 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);
                  }
           }
    }
}

Upvotes: 0

Views: 340

Answers (2)

gianni
gianni

Reputation: 1357

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

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31692

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).

Upvotes: 4

Related Questions