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);
}
}
}
}
filter()?matchedData = allData.filter(data => { return (data.type === 'store' ); });thenstores.push(matchedData);allData.slice(i)shifts all subsequent elements down (because you remove an element), which messes up your indexi.stores = stores.append(matchedData);instead ofstores.push(matchedData);?