I have an object db that has one field containing SKU's that correspond to another object that contains different venues.

db.SKU = [1002,1005,1001,1008,1007];

I then have another object listing the different venues and their details;

db.venueDB = [
        {
            SKU         : 1001,
            active      : 1,
            round       : 1,
            date        : "02/03/2026",
            country     : "Germany"
        },
        {
            SKU         : 1002,
            active      : 1,
            round       : 2,
            date        : "23/04/2026",
            country     : "Czechia"
        },{
            SKU         : 1003,
            round       : 3,
            active      : 1,
            date        : "05/05/2026",
            country     : "United Kingdom"
        },{
            SKU         : 1004,
            round       : 5,
            active      : 1,
            country     : "Spain",
            date        : "20/07/2026"
        },{
            SKU         : 1005,
            round       : 6,
            active      : 1,
            country     : "Sweden",
            date        : "21/07/2026"
        },{
            SKU         : 1006,
            active      : 1,
            round       : 7,
            country     : "Brazil",
            date        : "01/98/2026"
        },{
            SKU         : 1007,
            active      : 1,
            round       : 8,
            country     : "Latvia",
            date        : "08/09/2026"
        },{
            SKU         : 1008,
            round       : 9,
            active      : 1,
            country     : "Denmark",
            date        : "12/10/2026"
        }
    ]

Using the db.SKU I want to query db.venueDB to see if the SKU matches, and push that too a new filtered array containing the new details - in this case the new filtered array would be the venues with the SKU's, 1002, 1005, 1001, 1008 & 1007.

I know with this amount of data I could use a simple for/next loop but I have scaled down the data for this example.

I am not sure what is best or quickest way to do this, either using reduce or filter but unsure how to put this into practice.

6 Replies 6

Reduce() returns a single value, so I think that's probably not what you are looking for. Where are you stuck with using filter()?

using reduce or filter

Neither. Build up a lookup dictionary/map from your objects by SKU, then map over the SKU array. Add a filter step if you are not guaranteed to have a matching SKU.

const venueBySku = db
  .venueDB
  .values()
  .map((venue) => [venue.SKU, venue]);

const lookup = new Map(venueBySku);

const result = db.SKU
  .filter(sku => lookup.has(sku))
  .map(sku => lookup.get(sku));
  
console.log(result);
<script>
const db = {};

db.venueDB = [
    {
        SKU         : 1001,
        active      : 1,
        round       : 1,
        date        : "02/03/2026",
        country     : "Germany"
    },
    {
        SKU         : 1002,
        active      : 1,
        round       : 2,
        date        : "23/04/2026",
        country     : "Czechia"
    },{
        SKU         : 1003,
        round       : 3,
        active      : 1,
        date        : "05/05/2026",
        country     : "United Kingdom"
    },{
        SKU         : 1004,
        round       : 5,
        active      : 1,
        country     : "Spain",
        date        : "20/07/2026"
    },{
        SKU         : 1005,
        round       : 6,
        active      : 1,
        country     : "Sweden",
        date        : "21/07/2026"
    },{
        SKU         : 1006,
        active      : 1,
        round       : 7,
        country     : "Brazil",
        date        : "01/98/2026"
    },{
        SKU         : 1007,
        active      : 1,
        round       : 8,
        country     : "Latvia",
        date        : "08/09/2026"
    },{
        SKU         : 1008,
        round       : 9,
        active      : 1,
        country     : "Denmark",
        date        : "12/10/2026"
    }
];

db.SKU = [1002,1005,1001,1008,1007];
</script>

If you really need to, you can also use .reduce() to create the map, but seems a bit over the top:

const lookup = db.venueDB
    .reduce(
        (acc, venue) => acc.set(venue.SKU, venue), 
        new Map()
    );
// put in a set for O(1) membership checks
const skus = new Set(db.SKU)

// filter the database results by rows that have a SKU in `skus`
const result = db.venueDB.filter((row) => skus.has(row.SKU))

I can find one index using db.SKU but not sure how to or the correct code to find all the indexes

Using this code:

const venueBySku = db
  .venueDB
  .values()
  .map((venue) => [venue.SKU, venue]);

const lookup = new Map(venueBySku);

const result = db.SKU
  .filter(sku => lookup.has(sku))
  .map(sku => lookup.get(sku));
  
console.log(result);

But how would I then add additional filters, i.e also check that the active and return round number.

Thanks.

What debugging steps have you take with this code that only finds one index? Is venueBySku what you expect it to be? I don't think db.venueDB.values() returns what you think it does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.