Problem
I'm building a .json file for a search component of a used cars website. The file needs to be built from the output of two sections within CraftCMS, Used Cars and Vans & Trucks. It outputs the "make" of vehicle and it's "models".
My problem is that there are duplicate "makes"/"models" because they exist in both sections.
Code:
{% set allStock = craft.entries.section(['usedCars','vansTrucks']).level(2) %}
{% for stock in allStock %}
{% set models = stock.getChildren() %}
"{{ stock.make }}" : "{% for stock in models %}{{ stock.model }}{% if not loop.last %}, {% endif %}{% endfor %}"
{% endfor %}
Output:
"Holden" : "Commodore, Ute, Viva"
"Nissan" : "ATLAS, CARAVAN"
"Mazda" : "ATENZA, Axela, CX-7, MAZDA 6, MPV, RX-8, mpv"
"Nissan" : "CARAVAN, DUALIS, FAIRLADY, Liberty, Maxima, NAVARA, NOTE"
Updated
{% set allStock = craft.entries.section(['usedCars','vansTrucks']).level(2) %}
{% set newArray = [] %}
{% for stock in allStock %}
{% set models = stock.getChildren() %}
{% if stock.make not in newArray %}
"{{ stock.make }}" : "{% for stock in models %}{{ stock.model }}{% if not loop.last %}, {% endif %}{% endfor %}"
{% set newArray = newArray|merge([stock.make]) %}
{% endif %}
{% endfor %}
This code will remove the duplicate 'make' and merge but it also removes the models with it.
Output:
"Holden" : "Commodore, Ute, Viva"
"Mazda" : "ATENZA, Axela, CX-7, MAZDA 6, MPV, RX-8, mpv"
"Nissan" : "CARAVAN, DUALIS, FAIRLADY, Liberty, Maxima, NAVARA, NOTE"
Any help or direction on how to go about merging the models would be greatly appreciated.