0
[{"displayorder":"1","menuname":"DashBoard","menuid":"5","menuurl":"dashboard.php"},{"displayorder":"3","menuname":"Accounting Module","menuid":"3","menuurl":""},{"displayorder":"4","menuname":"My Profile","menuid":"4","menuurl":"myprofile.php"},{"displayorder":"6","menuname":"HR Module","menuid":"2","menuurl":""},{"displayorder":"9","menuname":"Administrator","menuid":"1","menuurl":""}]

I have here a stringfy json i want to know how to get a value of all the menuname in this json object any idea appreciated

UPDATE: I tried this one here but I get undefined in console

UPDATE

[{"displayorder":"1","menuname":"Menu Management","menuid":"1","submenuurl":"","parentid":"1"},{"displayorder":"1","menuname":"hr sub menu","menuid":"7","submenuurl":"error.php","parentid":"2"},{"displayorder":"2","menuname":"Role Management","menuid":"2","submenuurl":"","parentid":"1"},{"displayorder":"2","menuname":"menu 2 management2","menuid":"8","submenuurl":"","parentid":"2"},{"displayorder":"3","menuname":"hrsubmenu","menuid":"3","submenuurl":"contactus.php","parentid":"2"},{"displayorder":"3","menuname":"submenuaccounting","menuid":"4","submenuurl":"imagegallery.php","parentid":"3"}];

how to get all all details in the second json with parentid depending on above menuid?

2 Answers 2

1

Working example:

http://jsfiddle.net/0866pay3/

var json = [{"displayorder":"1","menuname":"DashBoard","menuid":"5","menuurl":"dashboard.php"},{"displayorder":"3","menuname":"Accounting Module","menuid":"3","menuurl":""},{"displayorder":"4","menuname":"My Profile","menuid":"4","menuurl":"myprofile.php"},{"displayorder":"6","menuname":"HR Module","menuid":"2","menuurl":""},{"displayorder":"9","menuname":"Administrator","menuid":"1","menuurl":""}];

    
json.forEach(function(el, idx){
    console.log(el.menuname);
});

Documentation Update

If you check out this article, you'll see the following:

callback is invoked with three arguments:

  • the element value
  • the element index
  • the array being traversed

So, idx is just a common way of representing the element index. You can call this whatever you'd like - theIndex, myRandomName, etc.

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

3 Comments

i need 8 mins to tick the answer
what is idx please explain
@HogRider See my Documentation Update on the bottom of the answer.
1
var myjson = [{"displayorder":"1","menuname":"DashBoard","menuid":"5","menuurl":"dashboard.php"},{"displayorder":"3","menuname":"Accounting Module","menuid":"3","menuurl":""},{"displayorder":"4","menuname":"My Profile","menuid":"4","menuurl":"myprofile.php"},{"displayorder":"6","menuname":"HR Module","menuid":"2","menuurl":""},{"displayorder":"9","menuname":"Administrator","menuid":"1","menuurl":""}]; 


var menu_names = []; 

for (var x = 0 ; x < myjson.length; x++){
    if(myjson[x].hasOwnProperty('menuname')){
        // do something usefull here 
        console.log(myjson[x]['menuname']);
        // add value to new array 
        menu_names.push(myjson[x]['menuname'])
    }
}

console.log(menu_names); 

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.