JSFiddle - React, Tailwind, and code Playground
HTML
<div class="info"></div>
<button>add</button>
JavaScript
const info = document.querySelector('.info')
const button = document.querySelector('button')
const cabins = [{
adult: 1,
teenager: 1,
babe: 1,
plug: 'plug',
plug1: 'plug',
plug2: 'plug',
},
{
adult: 2,
teenager: 0,
babe: 1,
plug: 'plug',
plug1: 'plug',
plug2: 'plug',
},
]
const withSeat = {
adult: 1,
teenager: 1,
babe: 0,
};
button.addEventListener('click', function() {
const passengers = cabins.flatMap(n => Object
.entries(n)
.flatMap(([k, v]) => Array.from({
length: v
},
() => ({
withSeat: withSeat[k],
id: Math.floor(Math.random() * 100),
})
))
);
info.append(JSON.stringify(passengers))
})