I have been struggling for a few days with that iteration. No matter what way I've used those methods I was only able to map or filter 'cars' array without further algorithms. I want to create a one array of objects, brands names, which would include drivers names, creating an array out of brand object.
I've tried mixing those methods in different ways and in different positions in my code but only thing I achieved is a mapped array with brands without array inside. I've also browsed the whole Stack to seek for a similar problem and exceptional solution.
var cars = [
{'brand': 'Ford'},
{'brand': 'Seat'},
{'brand': 'Opel'},
{'brand': 'Kia'},
{'brand': 'Mitsubishi'},
{'brand': 'Toyota'}
];
var drivers = [
{'name': 'Mark', 'car': 'Seat'},
{'name': 'John', 'car': 'Ford'},
{'name': 'Michael', 'car': 'Kia'},
{'name': 'Joe', 'car': 'Toyota'}
];
var assosiated = {};
console.log(assosiated);
That's what I tried of doing, but it doesn't meet my expectations:
assosiated = cars.map(function (carss) {
var driver = drivers.filter(function (dri) {
return carss.brand === dri.car;
});
return carss.brand;
}).reduce(function (prev, curr) {
return prev + '\n' + curr;
});
I expect the output to look like this:
assosiated = {
"Ford": [
{"name": "John"}
],
"Seat": [
{"name": "Mark"}
],
"Opel": [],
"Kia": [
{"name": "Michael"}
],
"Mitsubishi": [],
"Toyota": [
{"name": "Joe"}
]
}
I would also like to use all of those methods written above. Any idea what can I do to sort it like that?