0

I have an array that kind of looks like this:

result = {
   akch_generation: 11841,
   akch_chassis: [{
    akch_sp: [{
        akch_faulted: false,
        akch_present: true,
        akch_fru: 'hc:///chassis=0/sp=0'
    }],
    akch_fan: [{
   ....
 }

And I want to get to the value of akch_faulted, but I can't figure out how.

I tried:

hardware.config().akch_chassis.akch_sp => result = undefined

hardware.config().akch_chassis.akch_sp.akch_faulted =>
error: illegal argument expression: "hardware.config().akch_chassis.akch_sp has
   no properties"

where hardware.config() is the command I run to get the result array.

I can only get as deep as akch_chassis...

Can anybody help me?

2
  • 1
    result.akch_chassis[0].akch_sp[0].akch_faulted? Commented Nov 26, 2014 at 12:48
  • Yes! Thank you! It's a pitty you didn't reply where the answer section is... Commented Nov 26, 2014 at 15:04

3 Answers 3

2

First of all result is not an array, is an object.

The problem is that yours nested objects are inside arrays so to access the property akch_faulted you need to write this:

result.akch_chassis[0].akch_sp[0].akch_faulted
Sign up to request clarification or add additional context in comments.

6 Comments

already tried both of them, but it's not working: hardware.config().akch_chassis.akch_sp.akch_faulted error: illegal argument expression: "hardware.config().akch_chassis.akch_sp has no properties" hardware.config() gives me that object.
@Cristina could you post your whole code in a fiddle?
I'¡m afarid I can't. I run this command hardware.config() on a machine, and it displays this object (which is quite large).
I don't think that your comments are very useful ? By definition an array is an object, so there is not really "array notation" ? Did i said a mistake ?
I don't know if it was Saajan or you who answered first, but I'll give you the answer because you justified it well. Thanks for the solution.
|
1

This is the solution to get the value in javascript .

var myvalue = result.akch_chassis[0].akch_sp[0].akch_faulted

<script type="text/javascript" language="javascript" src="my.json"></script>
<script>
window.onload = function(){
var myvalue = result.akch_chassis[0].akch_sp[0].akch_faulted;
console.log(myvalue);
    }
</script>

1 Comment

Brilliant! It works! Thank you so much! Now I see the logics behind it.
0

This may be helpfull,

  for(var i=0;i<=result.akch_chassis.length;i++){
     for(var j=0;j<=result.akch_chassis[i].akch_sp.length;j++){
       var value=result.akch_chassis[i].akch_sp[j].akch_faulted;
     }
    }

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.