0

I have an array of arrays, where each inner array contains three items: A postcode, a street name and an array of dates [for garbage collection].

The user will provide their postcode and house number, and the appropriate information needs to be displayed to them. There's also a need to be able to add dates to each 'collection array' after carrying out checks.

This is the code I have, but the data seems badly structured. How could it be improved?

var postcodes = [
        ["BT161JR18", "Londonderry Park", [collectionArrayBT161JR18]], 
        ["BT161JR19",  "Ballyregan Cresent",  [collectionArrayBT161JR19]],
        ["BT161JR11", "Ballyregan Road",  [collectionArrayBT161JR11]],
    ]   ; 

var collectionArrayBT161JR18 = ["16/05/14", "18/06/14", "19/07/14"];
var collectionArrayBT161JR19 = ["17/05/14", "11/06/14"];
var collectionArrayBT161JR11 = ["14/05/14", "20/06/14"];
Carl Smith
  • 3,025
  • 24
  • 36
  • Please phrase a specific question you want help with. Questions should contain specific questions. You can read [here on asking a good question](http://stackoverflow.com/help/how-to-ask). – jfriend00 Nov 06 '14 at 23:16
  • I don't think you meant to put each collection array inside an otherwise empty array, but that's what you have, so `postcodes[0][2]` is an array with a collections array in, not an array of dates. – Carl Smith Nov 06 '14 at 23:19

3 Answers3

0

What you should do is to use arrays of objects instead arrays of arrays of arrays... For example same code could be designed like this:

var collectionArrayBT161JR18 = ["16/05/14", "18/06/14", "19/07/14"];
var collectionArrayBT161JR19 = ["17/05/14", "11/06/14"];
var collectionArrayBT161JR11 = ["14/05/14", "20/06/14"];

var postcodes = [
        {postcode: "BT161JR18", place: "Londonderry Park", dates: collectionArrayBT161JR18}, 
        {postcode: "BT161JR19", place: "Ballyregan Cresent", dates: collectionArrayBT161JR19},
        {postcode: "BT161JR11", place: "Ballyregan Road", dates: collectionArrayBT161JR11}
    ]   ; 

On this way you don't need to remember indexes (which you will forget over time), instead you use logical names.

Also notice that you need first to define dates and then to include them further in your postcodes structure.


Update to a comment: Searching for dates for given postcode is pretty straight forward.

//for your code
function findDates(pc /*postcode to search*/)
    for (var i=0; i<postcodes.length; i++) {
        if (postcodes[i][0] === pc)
            return postcodes[i][2][0]
    return false
}
//for suggested code
function findDates(pc /*postcode to search*/)
    for (var i=0; i<postcodes.length; i++) {
        if (postcodes[i].postcode === pc)
            return postcodes[i].dates
    return false
}
DRAX
  • 30,559
  • 2
  • 26
  • 28
  • Yes thank you, I did try this also. The problem I came across was the requirement to search the arrays using algorithms For example if the user enters BT161JR18, how do I display the dates information without knowing the index of the postcode? – user3592094 Nov 06 '14 at 23:30
  • Well you need to search through array until you find needed index. – DRAX Nov 06 '14 at 23:32
  • I want to check to see if what the postcode the user has entered exists in the array..do I need a parameter in the searchArray()? function searchArray(){ for (var i = 0; i < postcodes.length; i++) { if (postcodes[i].postcodes == searchValue) { index = i; alert('found'); break; } } } – user3592094 Nov 08 '14 at 15:13
0

You should use the postcodes as properties in an object to create the main container, instead of using an array. That way, you do not need to iterate over the array to find the postcode.

var collectionArrayBT161JR18 = ["16/05/14", "18/06/14", "19/07/14"];
var collectionArrayBT161JR19 = ["17/05/14", "11/06/14"];
var collectionArrayBT161JR11 = ["14/05/14", "20/06/14"];

var postcodes = {
    BT161JR18: {place: "Londonderry Park",   dates: collectionArrayBT161JR18}, 
    BT161JR19: {place: "Ballyregan Cresent", dates: collectionArrayBT161JR19},
    BT161JR11: {place: "Ballyregan Road",    dates: collectionArrayBT161JR11}
}
Carl Smith
  • 3,025
  • 24
  • 36
  • What your answer provides that mine already didn't? – DRAX Nov 06 '14 at 23:44
  • The correct data structure. – Carl Smith Nov 06 '14 at 23:47
  • It depends. If you need to iterate over structure then this will need special attention: http://stackoverflow.com/a/16735184 – DRAX Nov 06 '14 at 23:49
  • Now your clutching at straws. An object is the correct data structure here, and that's obvious. – Carl Smith Nov 06 '14 at 23:52
  • Actually it isn't. It would just make complications for example to take one object and for whatever reason to extract postcode. – DRAX Nov 06 '14 at 23:54
  • The OP states that the user will provide the postcode, so why would you iterate over the container? You only need the iterator code in your answer because you've used an array. – Carl Smith Nov 06 '14 at 23:54
  • Generally speaking, it is bad idea to store data like that. Your idea is ONLY good if you are directly accessing property like: `postocdes.BT161JR18` If you want dynamic access it is becomming boilerplate. I won't argue anymore about this because it is becoming long debate. – DRAX Nov 07 '14 at 00:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64448/discussion-between-drax-and-carl-smith). – DRAX Nov 07 '14 at 01:49
-4

setup an array in javascript

    var postcodes = new Array();
    var collectionArrayBT161JR18 = new Array();
    var collectionArrayBT161JR19 = new Array();
    var collectionArrayBT161JR11 = new Array();
good_idea
  • 1
  • 1
  • I'm new to javascript. This is what worked for me in using arrays. David, do you have something actually to offer - what is your "good idea"? – good_idea Nov 07 '14 at 18:54