2

I have a JSON like this:

{
        "myName":{
                 "forename":"alf",
                 "surname":"cool",
                 "phone":"000000000000",
                 "email":"mail@com"
},.....

My code where I can access each key

 for (var key in contacts) {
                          if (contacts.hasOwnProperty(key)) {
                                var newRow = new Element('li');
                                newRow.addClass('contact');
                                newRow.set('html', contacts[key].forename + ' ' + contacts[key].surname);

                                var innerSpan = new Element('span').set('html', contacts[key].phone + ', ' + contacts[key].email);
                                innerSpan.addClass('details');
                                innerSpan.set('html', contacts[key].phone + ', ' + contacts[key].email);
                                innerSpan.inject(newRow);

                                newRow.addEvent("click", this.setFromContact.bind(this, contacts[key]));
                                newRow.inject($(this.list));
                               // save myName to a variale here!!

                          }
                 }

Now I want to save "myName" to a variable.

4
  • 1
    And what's the problem you are dealing with ? Commented Jul 8, 2015 at 8:10
  • I dont't get how I can save myName from the json in a variable Commented Jul 8, 2015 at 8:11
  • any possibility to get the actual text of the key? Commented Jul 8, 2015 at 8:13
  • Do you want that in a variable or in a DOM element? Can you clearify the question a bit more? Commented Jul 8, 2015 at 8:45

3 Answers 3

2

Also, MooTools and ES5 provide Object.keys

var obj = {
    myName: { }
    yourName: { }
};

console.log(Object.keys(obj)); // ['myName', 'yourName']; 

I would also advise you to use Object.each(obj, fn(value, key, obj){}) for iterator. see http://mootools.net/core/docs/1.5.1/Types/Object#Object:Object-each

it gives you a closure so variables are not re-declared and dont interfere with each other.

var keys = [];
Object.each(contacts, function(value, key){
    var newRow = new Element('li.contact').set('html', '{forename} {surname}'.substitute(value)),
        innerSpan = new Element('span.details').set('html', '{phone}, {email}'.substitute(value)).inject(newRow);

    newRow.addEvent("click", this.setFromContact.bind(this, value)).inject($(this.list));
    keys.push(key); // avoids extra Object.keys call
}, this);
Sign up to request clarification or add additional context in comments.

Comments

1

You can test the value of the keys and if the value is "myName" you can save it into a variable.

var myName = '';
for (var key in contacts) {
    if(key == "myName")
        myName = key;
}

Comments

1
// Variable with the "myName" object    
var myVariable = contacts[key];

key variable is a string, such as "myName"

3 Comments

but I only want the actual text ("myName") of the key, not the whole object
What do you get if you do console.log(key)?
Okay, that was a stupid question of me. Thanks your advice with key made it!

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.