1

The question sounds weird I know. But its a weird question. Let me clarify.

Im using the facebook graph api. The feeds for my page are returned in JSON and i have this bit of JSON here:

"message": "A3Media Uk Website is fully up and running! Tell your friends, We can't make Beautiful Websites without clients!\n - Alex Morley-Finch",
"message_tags": {
    "116": [
        {
            "id": "514033508",
            "name": "Alex Morley-Finch",
            "offset": 116,
            "length": 17
        }
        ]
    }

So from my knowledge, there is an object called message_tags which contains and array of objects called 116, and index 0 of this array contains and object with the variables id, name, offset and length.

Now what i want to do is, obviously replace the text "Alex Morley-Finch" within the message variable with the tag "name": "Alex Morley-Finch". Then using the id, length and offset i can replace the text with a html link to that profile using the ID!

This all seem pretty simple, however, i obviously want my code to be dynamic so the code will work for ANY tag at ANY position.

The name of the object array "116" always matches the offset contained inside it.

The actual question:

How can I dynamically get the name of the object Array (in this case '116')?

Because my code would be something like (pseudo code):

if message has tag
    get name of tag
    if message contains name of tag
        replace message name using offset and length with html link tag with href = facebook url / id
    end if
end if

This would leave me with my html representation of the "message"

The thing is i cant get the name of the message_tag because id have to do somthing like:

// data[index] represents the current message

var json = JSON.parse(XmlHttpResponse.responseText);
json.data[index].message_tags.116[0].name;

as we can see this is not dynamic. This code will ONLY work for this tag.

So how do i get the name without referencing 116? is it even possible?

i was thinking about trying iterating through 1, 2, 3, 4, 5 --> 116 but that would be very performance costly AND bad coding AND im not even sure if you can reference arrays through variable names... ... ...

Im really stumped.

Please Help!

Alex

4
  • 1
    You can simply iterate over the properties of an object with a for...in loop. Side note: Your problem is actually not related to JSON but how to access properties of a JavaScript object. Commented Oct 20, 2011 at 10:24
  • Follow the link (for...in loop‌​ is a link). Commented Oct 20, 2011 at 10:28
  • but surely id still need to use the "116". for (var s in 116){} Commented Oct 20, 2011 at 10:30
  • No, you iterate over the properties of the object in message_tags. Commented Oct 20, 2011 at 10:31

2 Answers 2

2

This will loop through all of the tags. You might want to add code to check it's the one you want to change in case there are multiple tags.

for (var tag in message_tags) {
    message_tags[tag].name = "...";
}
Sign up to request clarification or add additional context in comments.

Comments

0

For..in solution

Use the for..in javascript statement to traverse your object properties and then act upon those results.

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.