ksd123
asked on
Get array elements with condition in javascript
Hi,
I am working on AngularJS and have a menu with 5 items ,here is a working pen menu that demonstrates my scenario, I took array of elements and passing item to the function. My requirement is I have to show menu items based on condition.ie. sometimes I can see only 2 items in the menu.How can I achieve in a efficient way in javascript.
Thanks in advance
I am working on AngularJS and have a menu with 5 items ,here is a working pen menu that demonstrates my scenario, I took array of elements and passing item to the function. My requirement is I have to show menu items based on condition.ie. sometimes I can see only 2 items in the menu.How can I achieve in a efficient way in javascript.
Thanks in advance
ASKER
Thank you for your response.I am using Angular 1.5 . Could you please elaborate the above explanation.
My requirement is , In the pen I took array of elements in javascript to show in menu (In reality the Menu items will be New, Delete, Update, Print etc.) . From API I get some data and based on condition the menu items will change.
Ex:
If condition1==true
showMenuItems=["NEw", "Print"];
If condition2==true
showMenuItems=["Update","P rint"]
If condition3==true
showMenuItems=["New"," Delete", "Update", "Print"]
My question is should I take 3 separate arrays as I have 3 conditions with different array of elements or should I add / remove elements in the array based on condition . It would be great if you provide sample code.
Thanks in advance
My requirement is , In the pen I took array of elements in javascript to show in menu (In reality the Menu items will be New, Delete, Update, Print etc.) . From API I get some data and based on condition the menu items will change.
Ex:
If condition1==true
showMenuItems=["NEw", "Print"];
If condition2==true
showMenuItems=["Update","P
If condition3==true
showMenuItems=["New"," Delete", "Update", "Print"]
My question is should I take 3 separate arrays as I have 3 conditions with different array of elements or should I add / remove elements in the array based on condition . It would be great if you provide sample code.
Thanks in advance
ASKER
Hi Experts,
Looking for some help on the above issue.
Looking for some help on the above issue.
var conditions = {
{ Option: “New”,
Condition: 1,
Option: “Print”,
Condition: 1,
},
{ Option: “Update”,
Condition: 2,
Option: “Print”,
Condition: 2,
},
}
Var filterCondition = 1;
You can filter the JSON data as
Var showMenuItems = Conditions.filter(function (item){ return item.Condition == filterCondition; })
{ Option: “New”,
Condition: 1,
Option: “Print”,
Condition: 1,
},
{ Option: “Update”,
Condition: 2,
Option: “Print”,
Condition: 2,
},
}
Var filterCondition = 1;
You can filter the JSON data as
Var showMenuItems = Conditions.filter(function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
You are welcome.
Why not use ng-show / ng-hide
https://docs.angularjs.org/api/ng/directive/ngShow
https://docs.angularjs.org/api/ng/directive/ngHide
Without knowing the specifics of your code and the conditions you want to hide show on - cannot provide a code solution - however if you do something like this
Open in new window
Or if you need more control over the conditionOpen in new window
And the in your controller you return true or false from the showitem function based on the criteria you want to show / hide on