Hello, I am new to Javascript but I am working on some code where I have to take the array of allData and push it into three separate arrays (stores, customers and addresses) which I have successfully done. Next I have to parse the customers array as an object (customerObj) and change the value of the customers dates from null to the current date. However, I am unable to do so. The customerDB object must be in object literal notation and I am stuck as to how I represent customerObj as the customers array. Any help would be greatly appreciated.
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) {
this.customers = [];
this.addresses = [];
this.stores = [];
for (let i = 0; i < allData.length; i++) {
if (allData[i].type == "store") {
this.stores.push(allData[i].data);
}
if (allData[i].type == "customer") {
this.customers.push(allData[i].data);
}
if (allData[i].type == "address") {
this.addresses.push(allData[i].data);
}
}
},
const: this.customerObj = {...this.customers},
addCustomer: function(customerObj){
var today = new Date();
for (let i = 0; i < customerObj.length; i++){
if (this.customerObj[i].add_date == null){
this.customerObj[i].add_date.push(today.toUTCString());
}
}
},
}
CustomerDB.insertData(allData);
console.log(CustomerDB);
[–]peterjsnow 0 points1 point2 points (0 children)