0

I have an $.each in which I have category IDs like 103 or 104 that are set with cat_id.

Now, inside that function, i want to run another $.each to access an array which has items that are sorted by those category IDs, like:

$.each( data.menu_items.items_CATID, function( key, val ) {

                    $.each( val, function( key2, val2 ) {
                        htm3+=''+val2.item_name+'..';
                    });


});

So basically, I need to access data.menu_items.items_103 or data.menu_items.items_104.

How do I set the variable? I tried with:

data.menu_items.items_'+cat_id+'

and such but that doesn't work.

The data is basically an array like:

menu_items:
items_251: (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
items_252: (6) [{…}, {…}, {…}, {…}, {…}, {…}]
3
  • We really need to see some more code, Commented Sep 25, 2019 at 7:21
  • Can you include the data in data.menu_items in your question, so we can see what you're working on? Commented Sep 25, 2019 at 7:27
  • @SimonBrahan Will do. Commented Sep 25, 2019 at 7:28

2 Answers 2

2

Try this

data.menu_items['items_' + cat_id]
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure of what you need but, have you tried :

const itemId = `items_${catId}`
data.menu_items[itemId]

I hope this helps !

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.