0

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.

0

1 Answer 1

1

You can take Object.values of the object and flat it:

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 ] } ]}

console.log(Object.values(mockCars).flat());

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.