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

jsBug
  • 348
  • 1
  • 9
  • 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
  • 35,834
  • 5
  • 42
  • 73
  • hoo this is too good man i just thought it have some logic but you nailed it with simple and neat code thanks man i really satisfied by your anwser – jsBug Jan 29 '22 at 09:36
  • @SRINIVASACHARYSUTHOJU Consider accepting the answer if you are satisfied by answer. – Maheer Ali Jan 30 '22 at 16:44
1

You could also use reduce:

const ingredients = saperatedIngredients.reduce( (acc, item) => {
 acc.push(...item.ingredients)
 return acc
}, [])
Radu Diță
  • 13,476
  • 2
  • 30
  • 34