0

I have a array called data inside that array I have objects. An object structure is like this

{
id:1,
especial_id:34,
restaurant_item:{id:1,restaurant:{res_name:'KFC'}}
}

I want to pass a res_name eg:- KFC

I want an output as a array which consists all the especial_ids

like this myarr = [12,23,23]

I could do something like this for that. But I want to know what is more elegant way to do this.

const data = [
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'McDonalds'}},
  
  {id:1,especial_id:8,restaurant_items:{id:5,res_name:'Kfc'}},
  
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'Sunmeal'}},
  
   {id:1,especial_id:6,restaurant_items:{id:5,res_name:'Kfc'}},
];

let temp = data.filter(element => element.restaurant_items.res_name == 'kfc')

let myArr = [];

temp.forEach(element=> myArr.push(element.especial_id));

console.log(myArr)//[8,6]

2
  • just use let myArry = data.filter(element => element.restaurant_items.res_name == 'kfc') Commented Nov 9, 2018 at 9:03
  • After "filter", instead of forEach you can directly use "map" to get especial ids. You can check my answer. Commented Nov 9, 2018 at 9:15

3 Answers 3

1

You can try this. It uses "Array.filter" and "Array.map"

var data = [
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'McDonalds'}},
  
  {id:1,especial_id:8,restaurant_items:{id:5,res_name:'Kfc'}},
  
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'Sunmeal'}},
  
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'Kfc'}},
];

function getEspecialIdsByName(name) {
  return data.filter(d => d.restaurant_items.res_name.toLowerCase() == name.toLowerCase())
             .map(d => d.especial_id)
}

console.log(getEspecialIdsByName('Kfc'))

console.log(getEspecialIdsByName('Sunmeal'))

Sign up to request clarification or add additional context in comments.

Comments

1

You can reduce to push elements which pass the test to the accumulator array in a single iteration over the input:

const data = [
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'McDonalds'}},
  {id:1,especial_id:8,restaurant_items:{id:5,res_name:'Kfc'}},
  {id:1,especial_id:6,restaurant_items:{id:5,res_name:'Sunmeal'}},
   {id:1,especial_id:6,restaurant_items:{id:5,res_name:'Kfc'}},
];
console.log(
  data.reduce((a, { especial_id, restaurant_items: { res_name }}) => {
    if (res_name === 'Kfc') a.push(especial_id)
    return a;
  }, [])
);

Comments

1

Use Array.reduce

const data = [{id:1,especial_id:6,restaurant_items:{id:5,res_name:'McDonalds'}},{id:1,especial_id:8,restaurant_items:{id:5,res_name:'Kfc'}},{id:1,especial_id:6,restaurant_items:{id:5,res_name:'Sunmeal'}},{id:1,especial_id:6,restaurant_items:{id:5,res_name:'Kfc'}}];

let result = data.reduce((a,c) => {
  if(c.restaurant_items.res_name === 'Kfc') a.push(c.especial_id);
  return a;
},[]);

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.