3
var saperatedIngredients =  [{
    ingredients: (2) ['Aar maach', 'Fish fillet']
    searchfilter: "marine fish"
    },
    {ingredients: ['Active dry yeast']
    searchfilter: "yeast"
    },
    {ingredients: (5) ['Ajwain', 'Cumin powder', 'Tamarind', 'Tamarind paste', 'Tamarind water']
    searchfilter: "miscellaneous derived edible product of plant origin"
    }
   ]

but i want the output like this

const IngredientsArray = ['Aar maach', 'Fish fillet', 'Active dry yeast','Ajwain', 'Cumin powder', 'Tamarind', 'Tamarind paste', 'Tamarind water']

anyone help me out for this problem

  • Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – derpirscher Jan 29 '22 at 09:30

2 Answers2

3

You can simply use flatMap()

var saperatedIngredients =  [{
    ingredients:  ['Aar maach', 'Fish fillet'],
    searchfilter: "marine fish"
    },
    {ingredients: ['Active dry yeast'],
    searchfilter: "yeast"
    },
    {ingredients:  ['Ajwain', 'Cumin powder', 'Tamarind', 'Tamarind paste', 'Tamarind water'],
    searchfilter: "miscellaneous derived edible product of plant origin"
    }
   ]
 
const res = saperatedIngredients.flatMap(x => x.ingredients);

console.log(res)
Maheer Ali
  • 34,163
  • 5
  • 36
  • 62
1

You could also use reduce:

const ingredients = saperatedIngredients.reduce( (acc, item) => {
 acc.push(...item.ingredients)
 return acc
}, [])
Radu Diță
  • 10,795
  • 1
  • 23
  • 31