Brownman Revival
Brownman Revival

Reputation: 3850

json stringify result get value on specific key

[{"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?

Upvotes: 0

Views: 178

Answers (2)

ratiorick
ratiorick

Reputation: 736

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); 

Upvotes: 1

What have you tried
What have you tried

Reputation: 11138

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.

Upvotes: 1

Related Questions