ReQL: Merging two objects based on a common property

14 Views Asked by At

I have one query that returns an array of objects that looks like this, where the same group of customers get returned every time (limiting my example to 2 customers for brevity):

[
  {
    "customer": "cust1",
    "date": 2023-03-03,
    "account_balance": 123.33,
  },
  {
    "customer": "cust2",
    "date": 2023-03-03,
    "account_balance": 55.10,
  }
]

I have a second query that returns an array of objects that is a subset of the customer list, where only customers with spend on a given day are returned. So if cust1 spends on a given day and cust2 doesn't, the second query would return:

[
  {
    "customer": "cust1",
    "date": 2023-03-03,
    "spend": 5.20,
  }
]

What I'm trying to do is end up with an array of objects, one for every customer, where I always see the account_balance for each customer; for customers with spend, that gets merged in from the second query, and for the rest I show spend:0. The result I'm trying to get using the example above is this:

[
  {
    "customer": "cust1",
    "date": 2023-03-03,
    "account_balance": 123.33,
    "spend": 5.20,
  },
  {
    "customer": "cust2",
    "date": 2023-03-03,
    "account_balance": 55.10,
    spend: 0
  }
]

Basically I just need to join these two objects together using the customer property, and I don't know how. I looked at merge and innerJoin|eqJoin|outerJoin in the Rethink documentation but I don't think those are what I want. merge just joins things together without matching on customer name, and join seems to only work on tables, and not the results of queries.

How do I do this?

0

There are 0 best solutions below