I want to be able to print an array of ICar objects (for now just printing to the console) by iterating through a TypeScript file populated with mock data.
The ICar model, car.model.ts:
export interface ICar {
Make: string;
Model: string;
Year: string;
Specifications: boolean[];
}
The mock data, car.mock.ts:
export const mockCars = {
New: [
{
Make: "Honda",
Model: "CRV",
Year: "2021",
Specifications: [
true,
true,
false,
true,
false
]
},
{
Make: "Toyota",
Model: "Camry",
Year: "2021",
Specifications: [
true,
true,
false,
true,
false
]
}
],
PreOwned: [
{
Make: "Subaru",
Model: "Forester",
Year: "2017",
Specifications: [
false,
true,
false,
false,
true
]
}
],
Used: [
{
Make: "Toyota",
Model: "Corolla",
Year: "2015",
Specifications: [
false,
false,
true,
true,
false
]
},
{
Make: "Honda",
Model: "CRV",
Year: "2011",
Specifications: [
true,
false,
false,
true,
true
]
},
{
Make: "Mazda",
Model: "CX-5",
Year: "2013",
Specifications: [
true,
true,
false,
false,
false
]
}
]
}
Right now, my current solution primarily focuses on the data value associated with the New key in the mock data:
ngOnInit() {
console.log(this.getTestCars());
}
getTestCars(): ICar[] {
var testArray = [];
var testClient = mockCars.New;
testClient.forEach(element => {
console.log(element.Model + " " + element.Make + " " + element.Year);
testArray.push(element);
});
return testArray;
}
And the output in the console from my solution:
CRV Honda 2021
Camry Toyota 2021
(2) [{…}, {…}]
0: {Make: "Honda", Model: "CRV", Year: "2021", Specifications: Array(5)}
1: {Make: "Toyota", Model: "Camry", Year: "2021", Specifications: Array(5)}
length: 2
__proto__: Array(0)
I want to be able to take all the objects from all the keys in the mock data (in this example, the values from New, PreOwned, Used) and push them into the array as ICar objects such that the array returns a length of 6.