0

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);
                  }
           }
    }
}
3
  • 1
    what about filter()? matchedData = allData.filter(data => { return (data.type === 'store' ); }); then stores.push(matchedData); Commented Feb 11, 2017 at 22:37
  • One problem is that allData.slice(i) shifts all subsequent elements down (because you remove an element), which messes up your index i. Commented Feb 11, 2017 at 22:38
  • @mickdev Wouldn't you want stores = stores.append(matchedData); instead of stores.push(matchedData);? Commented Feb 11, 2017 at 22:38

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.