2

I took a php array with double arguments (that has like ['contact']['en'] or ['contact']['fr'] or ['presentation']['en']) and did a json_encode to have its values in javascript (so I can use it for ajax)

when I do alert(JSON.stringify(myvariable, null, 4)); I get something like :

{
    "contact":{
        "fr":"value",
        "en":"value2",
        "es":"value3"
        },
    "presentation":{
        "fr":"value",
        "en":"value2"
        },
        etc...
}

What I want to do is to check if a value is equal to the first object (ex: contact=contact) then do a loop for that gets all the values when the condition is met. So if contact = contact, then get me the value of fr,en and es in contact. I managed to do the first object with:

for (var k in variable){
    if(k ==  valuechecked)
        {

        }                           
    }

But when I do inside if(k == valuechecked):

    for (var a in k){
        alert(a);
    }

It shows numbers from 0 up to I believe the amount of k there is (therefore the first object). I managed to do what I want with php but not with javascript ... How do I get the values of the objects inside the validated object?

4 Answers 4

5

It would be:

for (var a in variable[k]){
    alert(variable[k][a]);
}

k and a are strings (the names of the properties).

To avoid lots of repeated lookups, you'd probably save the object to a temp:

var entry = variable[k];
for (var a in entry){
    alert(entry[a]);
}

var variable = {
  "contact":{
    "fr":"contactvalue",
    "en":"contactvalue2",
    "es":"contactvalue3"
  },
  "presentation":{
    "fr":"presentationvalue",
    "en":"presentationvalue2"
  },
};
var valuechecked = "contact";
for (var k in variable){
  if(k ==  valuechecked)
  {
    var entry = variable[k];
    for (var a in entry){
      snippet.log(entry[a]);
    }

  }                           
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


Instead of for-in loops, another option (just to throw it out there) is:

Object.keys(variable).forEach(function(k) {
    if (k == valuechecked) {
        var entry = variable[k];
        Object.keys(entry).forEach(function(a) {
            // Use entry[a] here
        });
    }
});

var variable = {
  "contact":{
    "fr":"contactvalue",
    "en":"contactvalue2",
    "es":"contactvalue3"
  },
  "presentation":{
    "fr":"presentationvalue",
    "en":"presentationvalue2"
  },
};
var valuechecked = "contact";
Object.keys(variable).forEach(function(k) {
  if (k == valuechecked) {
    var entry = variable[k];
    Object.keys(entry).forEach(function(a) {
      snippet.log(entry[a]);
    });
  }
});
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

It's just handy because A) It only returns own properties, not inherited ones; and B) The iterator functions give you a nice contained scope for temps like entry. And if you do the same thing in other places, you can use named functions instead.

Note: Object.keys and Array#forEach are ES5 features present on all modern browsers. They can both be polyfilled on older browsers like IE8 if necessary.

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

3 Comments

nice and quick, I wanted to try it earlier but had a weird encoding bug! thanks!
tried it with the other option, strangely it doesn't work (at least with the alerts) even when I simply do: Object.keys(variable).forEach(function(k) { alert(variable[k])});
@user3595784: I've added live examples of both above. Both work, provided you're using a modern browser.
2
var obj = {
"contact":{
    "fr":"value",
    "en":"value2",
    "es":"value3"
    },
"presentation":{
    "fr":"value",
    "en":"value2"
    }
}

for(var a in obj) { 
    if(a== 'contact') {
       var b = obj[a];
       console.log("b is",  b);
       for(var c in b) {
        console.log("c is", c);
       }
    }
 }

this is one way in which you can solve this

Comments

0

This may be what you're looking for. What you can do is this First set the array as a far (which I'm sure you've done)

var array = {
"contact":{
    "fr":"value",
    "en":"value2",
    "es":"value3"
    },
"presentation":{
    "fr":"value",
    "en":"value2"
    },
    etc...
}

Here if contact exists. go through it.

for(first in array) {
    if (first == "contact") {
        // array.contact exists
        for(k in array.contact) {
             // k being key
             var thisValue = array.contact[k];
             console.log(thisValue);
        }
    }
}

Comments

0

There are several ways of doing this (see T.J. Crowder's answer) but if you know that you're after a specific item in the object you can just ask for it directly rather than looking for a match first:

//an example object:
    var myObject = {
        "contact":{
            "fr":"value",
            "en":"value2",
            "es":"value3"
            },
        "presentation":{
            "fr":"value",
            "en":"value2"
            }
    };
//get the part we are looking for:
    var obj = myObject["contact"];
//iterate over the object and alert each item:
    for (var item in obj){
        alert(item+" = "+obj[item]);
    }

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.