Get array elements with condition in javascript

Posted on 2016-08-03
Last Modified: 2016-08-13

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 sometimes I can see only 2 items in the menu.How can I achieve in a efficient way in javascript.

Thanks in advance
Question by:ksd123
  • 3
  • 3
LVL 54

Expert Comment

by:Julian Hansen
ID: 41742247
Angular 1.x or Angular 2?

Why not use ng-show / ng-hide

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

  <li ng-repeat="item in items" ngShow="item.value==condition">{{item.option}}</li>

Open in new window

Or if you need more control over the condition
  <li ng-repeat="item in items" ngShow="showItem(item)">{{item.option}}</li>

Open 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

Author Comment

ID: 41743605
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.


If condition1==true
showMenuItems=["NEw", "Print"];

If condition2==true

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

Author Comment

ID: 41745928
Hi Experts,

Looking for some help on the above issue.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Expert Comment

by:Srinivasulu Muppala
ID: 41753432
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; })
LVL 54

Accepted Solution

Julian Hansen earned 500 total points
ID: 41753530
I have created a new fork

This makes the following changes
<md-menu md-offset="0 -7">
  <md-button aria-label="Open demo menu" class="md-icon-button" ng-click="$mdOpenMenu($event)">
    <md-icon md-menu-origin="" md-svg-icon="call:chat"></md-icon>
  <md-menu-content width="2">
      ng-repeat="item in ctrl.menuItems" 
      ng-show="item.conditions.indexOf(ctrl.condition) != -1">

      <md-button  ng-click="ctrl.announceClick(item)"> <span md-menu-align-target=""></span> {{item.caption}}</md-button>


Open in new window

function DemoCtrl($mdDialog) {
  var vm = this;

  // condition determins what is shown
  vm.condition = 3;
  // menuItems array changed to be an array
  // of objects with a caption and an array
  // that specifies what conditions are 
  // valid for this menu item
    caption: "Option 1",
    conditions: [1,2]
    caption: "Option2",
    conditions: [2,3]
    caption: "Option3",
    conditions: [1,2,3]
    caption: "Option4",
    conditions: [2]
    caption: "Option5",
    conditions: [1,2,3,4]

Open in new window

You will see that the Angular code changes the definition of the menuItems so they are now an array of objects with a caption and an array showing what conditions the item is valid for.
The HTML has an ng-show that shows the item based on whether the current ctrl.condition value is contained in the items conditions array.

Author Closing Comment

ID: 41754576
Thank you
LVL 54

Expert Comment

by:Julian Hansen
ID: 41754621
You are welcome.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can i extend ? 2 42
How to make a moving character 3 55
Browser Chrome downloads colored link 5 40
How to print dynamic Google map and directions? 1 11
In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question