Solved

json javascript array question about  array referance

Posted on 2009-05-18
4
277 Views
Last Modified: 2013-11-08
Hi,


I am using JSON and eval to get an array of objects, like this:
{"location":[ {"loc_id":  "76",
                                    "description": "test",
                                    "company": "testrco",
then eval in javascript so var loc = eval........

The loc_id is a unique no., can i use this as the array no? I want to add to the array and change some of the values "description" for example would be god if i could reference it like
loc.location.loc[76].description instead of loc.location[2].description where the 2 doesn't relate to anything.

Hope I am making sense....

Thanks,
Ewan
0
Comment
Question by:ewan69
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:moagrius
ID: 24412939
natively, no - 76 is a value, not a key.  however, you could write a simple loop to create an array for reference and assign key by loc_id
var list = [];

for(var i=0;i<loc.location.length;i++){

var ref = loc.location[i];

var id = ref.loc_id;

list[id] = ref;

}

Open in new window

0
 

Author Comment

by:ewan69
ID: 24413183
Hi moagrius,

Thanks for the quick reply, sorry for not using the right terminoligy, still learing....

I will try your code in a minute thanks, is there a better way of doing things?

I want to be able to periodically update the array elements from a database so i was thinking if the PRIMARY KEY value was used as the array key, using eval i could easily replace the old element with the new.

Is there a better way of doing it?? maybe loop through the array and see if loc.location[i].loc_id == the loc_id of the update, if it does then replace that element with the new one.

not sure if element is the right word, i mean one of the whole objects in the array.

Thanks,

Ewan
0
 
LVL 19

Accepted Solution

by:
moagrius earned 500 total points
ID: 24413476
Looping through the array and checking the loc_id is a good approach too - either way works really.  Just a note, in the above example code I gave before, you'd then look up the element using list[76] - "list" would contain the order copy of the location array.
0
 

Author Closing Comment

by:ewan69
ID: 31582630
Thanks for your help... :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 36
Polygon I google maps 1 16
Message not shown 5 33
Randomize in Owl Carousel v1.3.2 6 20
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now