Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object as an array HELP! 1

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
Hi there,

I am using an array of objects to simulate an associative array, it is declared as follows:

Code:
nan12h = { "0": { "count": "24", "name": "Cash in the Attic", "starttime": "11:30", "startstamp": "1218108600", "endtime": "12:15", "endstamp": "1218111300", "description": "Sheila Long\'s daughter is planning to go to New Zealand. Before she heads off, the family wants to raise money for a walking weekend together in the Lake District. [S] Then BBC News." }, "1": { "count": "24", "name": "Bargain Hunt", "starttime": "12:15", "startstamp": "1218111300", "endtime": "13:00", "endstamp": "1218114000", "description": "The bargain hunters are scouring an antiques fair in the grounds of Deene Park near Corby, Northamptonshire, aided by the two expert Davids, Barby and Harper. Tim Wonnacott presents. [S]" }, "2": { "count": "24", "name": "BBC News at One", "starttime": "13:00", "startstamp": "1218114000", "endtime": "13:30", "endstamp": "1218115800", "description": "National and international news from the BBC, followed by Weather. [S]" }}

So I can the reference the array using nan12h[0]['name'] etc.

Later on I want to iterate through the array and remove any items that have finished using this code:

Code:
if (newArr[x]['endstamp'] < nowtime) {
	delete newArr[x];
}

This works fine except that the array now starts from 7 instead of 0. Is there any simple way to reorder the array at this point or is it a case of writing a custom function?

G

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
If I understand correctly, I think you might be confused. The array isn't starting from 7 at all, but the keyed field used to pull back the data potentially holds value theat are no longer consecutive (and possibly that do not start at '0'). As I understand it, you want to renumber these so they are consecutive and do start at '0'.

Because you are using a number as the key instead of having something like 'id' as the key and a number as the key's value, it's going to be hard to rename without duplicating the data, I think.

Personally, I'd either make they key the value (see above para), or iterate over the data (using for..each) and copying any items you want to keep into a new data structure with a new (ordered) key, and then delete the old data altogether.

P.S. This looks very much like the data behind Sky's new EPG... This isn't for that project, is it?

Hope this helps,
Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for the reply, I have used a work-around for now with a view to reviewing the data structure.

The data is indeed for an EPG, but not for Sky ;).

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top