Javascript 使用函数式编程连接两个JSON对象数组,如SQL连接

Javascript 使用函数式编程连接两个JSON对象数组,如SQL连接,javascript,functional-programming,ramda.js,Javascript,Functional Programming,Ramda.js,考虑一下,我有以下两个对象数组: const existingAndArchivedBookings = [ {"booking_id":-2}, {"booking_id":-1}, {"booking_id":999} ] const newAndExistingBookings = [ {bookingId:-2, name: "name1"}, {bookingId:-3, name: "name1"}, {bookingId:-1, name: "namex"}

考虑一下,我有以下两个对象数组:

const existingAndArchivedBookings = 
[
 {"booking_id":-2},
 {"booking_id":-1},
 {"booking_id":999}
]

const newAndExistingBookings = 
[
 {bookingId:-2, name: "name1"}, 
 {bookingId:-3, name: "name1"}, 
 {bookingId:-1, name: "namex"}
]
我要做的是确定第二个数组中的哪些预订是新的,哪些是现有的。两个数组中的任何bookingId都存在。第二个数组中但不是第一个数组中的任何bookingID都是新的。因此,解决方案的结果应该是一个数组,如下所示:

[ { bookingId: -2, existing: true, name: 'name1' },
  { bookingId: -3, existing: false, name: 'name1' },
  { bookingId: -1, existing: true, name: 'namex' } ]

我有一个解决方案(我将作为答案发布),但我认为可能有一种更有效的方法。祝你好运

这是我的想法,似乎有些冗长

const R = require('ramda')

const existingAndArchivedBookings = [{"booking_id":-2},{"booking_id":-1},{"booking_id":999}]
const newAndExistingBookings = [{bookingId:-2, name: "name1"}, {bookingId:-3, name: "name1"}, {bookingId:-1, name: "namex"}]

const existingAndArchivedKeys = existingAndArchivedBookings.map(value => value.booking_id)
const newAndExistingKeys = newAndExistingBookings.map(value => value.bookingId)

const existingKeys = existingAndArchivedKeys.filter(key => newAndExistingKeys.includes(key))
const newKeys = newAndExistingKeys.filter(key => !existingAndArchivedKeys.includes(key))

const existingBookingIds = existingKeys.map(key => {
    return {bookingId: key, existing: true}
})

const newBookingIds  = newKeys.map(key => {
    return {bookingId: key, existing: false}
})


const allArray = R.concat(newAndExistingBookings, R.concat(existingBookingIds, newBookingIds))

console.log(R.values(R.reduceBy(R.mergeLeft, {}, R.prop('bookingId'), allArray)))

您可以使用
Array.prototype.reduce
来大大简化它,以形成两个数组之间的比较结果,并使用
Array.prototype.findIndex
来测试第二个数组中的对象是否存在于第一个数组中:

const existingandarchivebookings=
[
{“预订id”:-2},
{“预订id”:-1},
{“预订id”:999}
]
const newAndExistingBookings=
[
{bookingId:-2,名称:“name1”},
{bookingId:-3,名称:“name1”},
{bookingId:-1,名称:“namex”}
]
const res=新的和现有的预订。减少((acc,ele)=>{
const idx=现有和存档的bookings.findIndex(b=>b.booking\u id==ele.bookingId);
让现有=假;
如果(idx>=0){
存在=真实;
}
返回acc.concat({bookingId:`${ele.bookingId}`,现有:`${existing}`,name:`${ele.name}`);
}, []);

控制台日志(res)如果您想要一个非R答案:您可以使用一个简单的方法对数据进行迭代,比较两个数组中的预订ID(使用),然后返回一个新的对象数组

const existingandarchivebookings=[{booking_id:-2},{booking_id:-1},{booking_id:999}];
const newAndExistingBookings=[{bookingId:-2,名称:“name1”},{bookingId:-3,名称:“name1”},{bookingId:-1,名称:“namex”}];
功能测试预订(arr1、arr2){
返回arr2.map({bookingId,name})=>{
const existing=arr1.some(obj=>obj.booking\u id==bookingId);
返回{bookingId,existing,name};
});
}
const out=测试预订(现有和存档预订、新建和现有预订);

控制台。注销一些< /代码>函数。我会给它一个小时左右。。如果没有人能想出更好的答案,那你就知道了!我现在想不出任何比这更具可读性的Ramda解决方案。美好的