2

I have an JSON object in javascript

"urls": {"url": "http://www.google.co.uk"}

I want to be able to get the actual URL google.co.uk, not the text 'url'.

I also have a variable called date.

I want to create a New object that holds the url value as the key and set the value to the date '20/10/2013' for example.

"newObject": {"http://www.google.co.uk": "20/10/2013"}

Im sure this is possible however I am not so good with json objects and i welcome any help.

3
  • your syntax is a little off. Are these complete objects? Commented Mar 11, 2014 at 17:51
  • This is a bit confusing as neither of your snippets are valid JSON. Is urls an object property, or the name of your Javascript variable? Same question for newObject -- do you want one of the object properties to be newObject, or is that just what you want the name of the variable to be? Commented Mar 11, 2014 at 17:52
  • urls and newObject are the names of the object Commented Mar 11, 2014 at 17:55

2 Answers 2

5

IMHO, you should create a readable array like this

var myArray = [
  { url: "http://www.google.co.uk", date: "20/10/2013"},
  { url: "http://www.google.com", date: "20/10/2014" }
]

then you can parse all elements using a for loop for each object in the array.

var myObject;
var url, date;
for(var k = 0; k < myArray.length; k++)
    myObject = myArray[k];
    url = myObject.url;
    date = myObject.date;
}

If you need speed you can use a Memoization Pattern or create hashes to get O(1) queries.

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

2 Comments

Shouldn't use a for ... in ... loop on an array.
You should use a "normal" for(var i = 0; i < myArray.length; i++) loop here. for..in is not meant to be used on arrays.
2

To get the URL, you just have yo do :

var myURL = urls.url;

Then to get the date :

var myDate = newObject[myURL];

3 Comments

Would that then associate a url with a particular date?
@Joe could you explain more what you mean? If you have the URL, you can have the date.
Do you know much about google drive api using javascript? I am currently stuck retreiving picture URLs

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.