I am working with two arrays of objects and I need to determine how many different types of cars we have.
The first array contains the IDs of all the cars, while the second array contains information about the types of cars.
Here is the data:
var arr = {
"categories": [{
"id": "100",
"name": "category name",
"car_id": "1"
}, {
"id": "192",
"name": "category name here",
"car_id": "25"
}, {
"id": "192",
"name": "category name here",
"car_id": "27"
}]
};
var arr2 = {
"cars": [{
"id": "1",
"name": "car name",
"car_id": "1",
"type": "ford"
}, {
"id": "4",
"name": "name 2",
"car_id": "25",
"type": "ford"
}, {
"id": "4",
"name": "name 2",
"car_id": "27",
"type": "fiat"
}]
};
Since there are only 5 types of cars, I have created 5 variables:
var:
ford,
fiat,
mazda,
mini,
mg
My goal is to end up with something like this:
ford: 2;
fiat: 1;
mazda: 0;
mini: 0;
mg: 0;
Can someone guide me on how to achieve this?