Is anyone online? I need help. I have an array structured like this.
var array = [ {
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Hot Water',
additional_facilities: 'Iron' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Minibar',
additional_facilities: 'AC' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'cold Water',
additional_facilities: 'Fan' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View',
additional_facilities: 'Washing' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View 2',
additional_facilities: 'wash' }
]
I want to transform it into the following format.
var result =[{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: ['Hot Water','Minibar','cold Water'],
additional_facilities: ['Iron','AC','fan'] },{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: ['View','View 2'],
additional_facilities: ['Washing','wash'] }]
I understand this is a fundamental task, but I require assistance with it. Thank you.
This is what I have accomplished thus far.
var array = [ {
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Hot Water',
additional_facilities: 'Iron' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'Minibar',
additional_facilities: 'AC' },
{
roomNumber: 'R01',
roomType: 'Deluxe',
basic_facilities: 'cold Water',
additional_facilities: 'Fan' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View',
additional_facilities: 'Washing' },
{
roomNumber: 'R02',
roomType: 'standerd',
basic_facilities: 'View 2',
additional_facilities: 'wash' }
]
result = [];
array.forEach(function (a) {
if (!this[a.roomNumber]) {
this[a.roomNumber] = { roomNumber: a.roomNumber};
result.push(this[a.roomNumber]);
}
}, Object.create(null));
console.log(result);