0

Consider, I have the following two arrays of objects:

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

What I want to do is determine which of the bookings in the second array are new and which are existing. Any bookingId that is in both arrays is existing. Any bookingID that is in the second array but not the first is new. So, the result of the solution should be an array as follows:

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

I have a solution (which I'll post as an answer), but I think there's probably a more efficient way of doing it. Good luck.

1
  • You haven’t fulfill your promise yet. Commented Aug 23, 2019 at 17:53

3 Answers 3

4

If you want a non-R answer: you can use a simple map to iterate over the data, compare the booking ids in both arrays (with some), and return a new array of objects.

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

function testBookings(arr1, arr2) {
  return arr2.map(({ bookingId, name }) => {
    const existing = arr1.some(obj => obj.booking_id === bookingId);
    return { bookingId, existing, name };
  });
}

const out = testBookings(existingAndArchivedBookings, newAndExistingBookings);

console.log(out);

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

2 Comments

very concise, I didn't know about the some function. I'm going to give it about an hour.. if no one comes up with a better answer you've got it!
I can't immediately think of any Ramda solution that would be more readable than this. Nice!
2

You can greatly simplify it using Array.prototype.reduce to form the result of the comparisons between the 2 arrays and Array.prototype.findIndex to test whether the object in the second array is present in the first array:

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 res = newAndExistingBookings.reduce((acc, ele) => {
  const idx = existingAndArchivedBookings.findIndex(b => b.booking_id === ele.bookingId);
  let existing = false;
  if(idx >=0 ){
    existing = true;
  }
  return acc.concat({bookingId : `${ele.bookingId}`, existing: `${existing}`, name: `${ele.name}`});
}, []);
console.log(res);

Comments

0

Here's what I came up with, which seems a bit long winded

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

3 Comments

Because this is so much more readable than a forEach or even reduce oneliner
@mplungjan: Note that this was the OP posting, as promised, his working but ugly solution.
@JohnHarrison: next time, please post your work inside the question. Answering it yourselft either to summarize the responses and/or to show that you eventually got it is fine. But, while there is a bit of gamification here, this is not a contest; it's simply a Q & A site. Also, if you already have a working solution that you're looking to see improved, consider posting it to Code Review instead.

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.