3

So I have two array of JSON objects, rooms, and reservations. I want to output a new array of objects that has data combined from both those arrays with a new data type. Not sure how to create that new data type.

var rooms = [
     {id:1, room:'treehouse'},
     {id:2, room:'casa'},
     {id:3, room:'vacation'},
     {id:4, room:'presidential'}
];

var reservations = [
  {id:1, roomID:'2', time:'2pm', location:'rome'},
  {id:2, roomID:'3', time:'4pm', location:'paris'},
  {id:3, roomID:'1', time:'4pm', location:'london'},
  {id:4, roomID:'2', time:'7pm', location:'rome'},
  {id:5, roomID:'1', time:'12pm', location:'london'},
  {id:6, roomID:'4', time:'4pm', location:'berlin'}
];

Desired Output:

var bookings = [
  {id: 1, roomid:1, time:'12pm',location:'london', roomname:'treehouse'},
  {id: 2, roomid:1, time:'4pm', location:'london', roomname:'treehouse'},
  {id: 3, roomid:2, time:'2pm', location:'rome', roomname:'casa'},
  {id: 4, roomid:2, time:'7pm', location:'rome', roomname:'casa'},
  {id: 5, roomid:3, time:'4pm', location:'paris', roomname:'vacation'},
  {id: 6, roomid:4, time:'4pm', location:'berlin', roomname:'presidential'}
]

I am confused on the logic on how to do this. I was thinking of iterating over the reservations array and for each reservation grab roomId and check in a map structure of rooms and then output. I'm not quite sure what to do.

1
  • is it intended roomid vs roomID? and why is the type different one is a number, one is a string. Commented Sep 25, 2018 at 17:13

6 Answers 6

1

var rooms = [
     {id:1, room:'treehouse'},
     {id:2, room:'casa'},
     {id:3, room:'vacation'},
     {id:4, room:'presidential'}
];

var reservations = [
  {id:1, roomID:'2', time:'2pm', location:'rome'},
  {id:2, roomID:'3', time:'4pm', location:'paris'},
  {id:3, roomID:'1', time:'4pm', location:'london'},
  {id:4, roomID:'2', time:'7pm', location:'rome'},
  {id:5, roomID:'1', time:'12pm', location:'london'},
  {id:6, roomID:'4', time:'4pm', location:'berlin'}
];

console.log(
  reservations.map(a=>({...a, roomname: rooms.find(b=>b.id==a.roomID).room}))
)

Sign up to request clarification or add additional context in comments.

Comments

0

You could tale the power of a Map and get the additional information for a new object.

var rooms = [{ id: 1, room: 'treehouse' }, { id: 2, room: 'casa' }, { id: 3, room: 'vacation' }, { id: 4, room: 'presidential' }],
    roomMap = new Map(rooms.map(({ id: roomid, room: roomname }) => [roomid, { roomname }])),
    reservations = [{ id: 1, roomID: '2', time: '2pm', location: 'rome' }, { id: 2, roomID: '3', time: '4pm', location: 'paris' }, { id: 3, roomID: '1', time: '4pm', location: 'london' }, { id: 4, roomID: '2', time: '7pm', location: 'rome' }, { id: 5, roomID: '1', time: '12pm', location: 'london' }, { id: 6, roomID: '4', time: '4pm', location: 'berlin' }],
    bookings = reservations.map(o => Object.assign({}, o, roomMap.get(+o.roomID)));

console.log(bookings);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

You could use

reservations.map(function (val) {
    return Object.assign(val, rooms.find(function (item) {
        return val.roomID === item.id ? item : {}
    }))
})

Let me know if that works for you.

Comments

0

You can use Array.reduce() to create a map of id map with room name and then use Array.map() on reservations to merge the room with given id with roomname:

var rooms = [ {id:1, room:'treehouse'}, {id:2, room:'casa'}, {id:3, room:'vacation'}, {id:4, room:'presidential'} ];

var reservations = [ {id:1, roomID:'2', time:'2pm', location:'rome'}, {id:2, roomID:'3', time:'4pm', location:'paris'}, {id:3, roomID:'1', time:'4pm', location:'london'}, {id:4, roomID:'2', time:'7pm', location:'rome'}, {id:5, roomID:'1', time:'12pm', location:'london'}, {id:6, roomID:'4', time:'4pm', location:'berlin'} ];

let roomMap = rooms.reduce((a,curr)=>{
  a[curr.id] = {roomname : curr.room};
  return a;
},{});
let result = reservations.map((o)=> Object.assign({},o,roomMap[o.roomID]));
console.log(result);

Comments

0

var reservations = [
  {id:1, roomID: 2, time:'2pm', location:'rome'},
  {id:2, roomID: 3, time:'4pm', location:'paris'},
  {id:3, roomID: 1, time:'4pm', location:'london'},
  {id:4, roomID: 2, time:'7pm', location:'rome'},
  {id:5, roomID: 1, time:'12pm', location:'london'},
  {id:6, roomID: 4, time:'4pm', location:'berlin'}
];

var rooms = [
     {id:1, room:'treehouse'},
     {id:2, room:'casa'},
     {id:3, room:'vacation'},
     {id:4, room:'presidential'}
];
reservations.forEach(reservation => {
  reservation.roomname = rooms.find(({id}) => id === reservation.roomID).room;
})

console.log(reservations);

Comments

0

You can create a lookup has with Array.prototype.reduce and Array.prototype.map to iterate over the reservations array and construct the desired output:

var rooms = [{id:1, room:'treehouse'},{id:2, room:'casa'},{id:3, room:'vacation'},{id:4, room:'presidential'}];

var reservations = [{id:1, roomID:'2', time:'2pm', location:'rome'},{id:2, roomID:'3', time:'4pm', location:'paris'},{id:3, roomID:'1', time:'4pm', location:'london'},{id:4, roomID:'2', time:'7pm', location:'rome'},{id:5, roomID:'1', time:'12pm', location:'london'},{id:6, roomID:'4', time:'4pm', location:'berlin'}];

var roomHash = rooms.reduce((all, {id, room}) => ({...all, [id]: room }), {});

var result = reservations.map(item => ({...item, room: roomHash[item.roomID]}));

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.