JS使用新键创建对象的新数组

时间:2018-09-25 17:07:17

标签: javascript arrays

所以我有两个数组的JSON对象,房间和预订。我想输出一个对象的新数组,该数组具有从这两个数组合并的数据并具有新的数据类型。不确定如何创建该新数据类型。

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 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'}
]

我对如何执行此操作的逻辑感到困惑。我正在考虑遍历reservations数组,并为每个预订获取roomId并检入房间的地图结构,然后输出。我不太确定该怎么办。

6 个答案:

答案 0 :(得分: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}))
)

答案 1 :(得分:0)

您可以使用Array.reduce()创建带有房间名称的id地图,然后在预订中使用Array.map()将具有给定ID和房间名称的房间合并:

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);

答案 2 :(得分:0)

您可以了解Map的功能,并获取有关新对象的其他信息。

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; }

答案 3 :(得分: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);

答案 4 :(得分:0)

您可以使用

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

让我知道这是否对您有用。

答案 5 :(得分:0)

您可以使用Array.prototype.reduceArray.prototype.map创建一个具有reservations数组迭代的查找并构造所需的输出:

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);