Extract string list as array from an array of objects Having the following array of objects of ingredients used by cooks
images = [{id:1, cook:&quot;jim&quot;,   ingredients:&quot;chicken salsa mayo ketchup tomato&quot;},
          {id:2, cook:&quot;steve&quot;, ingredients:&quot;pasta mozzarela chicken&quot;},
          {id:3, cook:&quot;jim&quot;,   ingredients:&quot;bread lettuce tomato onions&quot;},
          {id:4, cook:&quot;jim&quot;,   ingredients:&quot;ketchup sausage bread&quot;},
          {id:5, cook:&quot;jim&quot;,   ingredients:&quot;avocado ground_meat chili&quot;},
          {id:6, cook:&quot;jim&quot;,   ingredients:&quot;ground_meat guacamole tomato parsley&quot;},
          {id:7, cook:&quot;steve&quot;, ingredients:&quot;chicken tomato broccoli&quot;}]

I have to extract an array of ingredients used by a specific cook so that each ingredient appears only once, return only 25 ingredients unless a specific limit is provided.
extractIngredients(array, cook, limit=25)
e.g:
extractIngredients(images, &quot;jim&quot;, 10) ==&gt; [&quot;chicken&quot;, &quot;salsa&quot;, &quot;mayo&quot;, &quot;ketchup&quot;, &quot;tomato&quot;, &quot;bread&quot;, &quot;lettuce&quot;, &quot;onions&quot;, &quot;hotdog&quot;, &quot;avocado&quot;]
BONUS POINTS:
Return an ingredient array sorted by the number of occurrences, and then alphabetically, showing the most popular ingredients first.
e.g:
extractIngredients(images, &quot;jim&quot;, 10) ==&gt; [&quot;tomato&quot;, &quot;bread&quot;, &quot;ground_meat&quot;, &quot;ketchup&quot;, &quot;avocado&quot;, &quot;chicken&quot;, &quot;chili&quot;, &quot;guacamole&quot;, &quot;lettuce&quot;, &quot;mayo&quot;]
My solution:
extractIngredients(images, username, limit){
  return [...new Set(images.filter(images =&gt; images.cook == username)
       .map(a =&gt; a.keywords.split(&quot; &quot;)).flat())].slice(0, limit);
}

My solution has an issue with an extremely large input as it would still process all of the objects and then slice the resulting array. It would be better to just process objects until the limit is reached.
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.