Extract string list as array from an array of objects
Sounds like a perfect case for generator functions. Unfortunately, map and the other array default methods don't have lazy versions of map/filter, but these functions aren't hard to write.


const images = [
    {id:1, cook:"jim",   ingredients:"chicken salsa mayo ketchup tomato"},
    {id:2, cook:"steve", ingredients:"pasta mozzarela chicken"},
    {id:3, cook:"jim",   ingredients:"bread lettuce tomato onions"},
    {id:4, cook:"jim",   ingredients:"ketchup sausage bread"},
    {id:5, cook:"jim",   ingredients:"avocado ground_meat chili"},
    {id:6, cook:"jim",   ingredients:"ground_meat guacamole tomato parsley"},
    {id:7, cook:"steve", ingredients:"chicken tomato broccoli"}
]

function* lazyMap(iterator,callback) {
    for (const value of iterator) {
        yield callback(value)
    }
}

function* lazyFilter(iterator,callback) {
    for (const value of iterator) {
        console.log(`looking at order with id=${value.id}`)
        if (callback(value)) yield value
    }
}

function* lazyFlat(iterator) {
    for (const list of iterator) {
        for (const value of list) {
            yield value
        }
    }
}

function* lazyUnique(iterator) {
    const seen = new Set()
    for (const value of iterator) {
        if (!seen.has(value)) yield value
        seen.add(value)
    }
}

function takeSome(iterator,limit) {
    let list = []
    for (const value of iterator) {
        list.push(value)
        if (list.length&gt;=limit) return list
    }
    return list
}

function extractIngredients(orders,cook,limit=25) {
    const cook_orders = lazyFilter(orders,order=&gt;order.cook===cook)
    const orders_ingredients = lazyMap(cook_orders,order=&gt;order.ingredients.split(' '))
    const all_ingredients = lazyFlat(orders_ingredients)
    const unique_ingredients = lazyUnique(all_ingredients)
    return takeSome(unique_ingredients,limit)
}

console.log(extractIngredients(images,'jim',10))



As you can see from the console output, only IDs up to 5 are looked at, because on the order with ID=5, the 10th ingredient is found and the function returns.
The bonus seems confusing, as if your goal is to sort by frequency, you must traverse all of your data to build an accurate histogram.