Search code examples
javascriptarraysrecords

Append variable and array items after a record array search


I would like to update a variable and array item after a record array search, and wanted to know what would be the best way of carrying it out in Javascript.

In this case, if a match is found between the variable and area item, then the postcode item should be appended to the variable.

The same needs to be done for the array items, but I reckon that a for loop would work best in this scenario, as each individual item would need to be accessed somehow.

Btw due to the nature of my program, there will always be a match. Furthermore, I need to be able to distinguish between the singleAddress and multipleAddresses variables.

Hopefully this code explains things better:

// before search 
var singleAddress = "Mount Farm";
var multipleAddresses = ["Elfield Park", "Far Bletchley", "Medbourne", "Brickfields"];

// this is the record which the search needs to be run against
plot = [{
    postcode: "MK1",
    area: "Denbigh, Mount Farm",
}, {
    postcode: "MK2",
    area: "Brickfields, Central Bletchley, Fenny Stratford, Water Eaton"
}, {
    postcode: "MK3",
    area: "Church Green, Far Bletchley, Old Bletchley, West Bletchley",
}, {
    postcode: "MK4",
    area: "Emerson Valley, Furzton, Kingsmead, Shenley Brook End, Snelshall West, Tattenhoe, Tattenhoe Park, Westcroft, Whaddon, Woodhill",
}, {
    postcode: "MK5",
    area: "Crownhill, Elfield Park, Grange Farm, Oakhill, Knowlhill, Loughton, Medbourne, Shenley Brook End, Shenley Church End, Shenley Lodge, Shenley Wood",
}]

// after search is run then:
// var singleAddress = "Mount Farm, MK1"
// var multipleAddresses = ["Elfield Park, MK5", "Far Bletchley, MK3", "Medbourne, MK5", "Brickfields, MK2"]

Fiddle here


Solution

  • All right, so you can't easily change the original data format. How big is the actual record and how often does it update? 'cause if it's not huge and doesn't change several times per second and you're going to do many lookups, it would be more efficient to create the mapping I mentionned and then read from it, than to look through the original structure every time. I'm going to assume so in this answer. Hope it helps.

    Another assumption in this code: that each area has exactly one postcode.

    function convertPlot(plot)
    {
        var mapping={}
        plot.forEach(function (r){
            var locations=r.area.split(/,\s*/)
            for(var i=0, loc; loc=locations[i]; i++)
                mapping[loc]=r.postcode
        })
        return mapping
    }
    

    Simply do this when you get the plot:

    mapping=convertPlot(plot)
    

    Then the lookup function becomes trivial:

    function lookup(adr)
    {
        if('string' == typeof adr)
            return adr+", "+mapping[adr]
        if(('object' == typeof adr) && adr instanceof Array)
            return adr.map(lookup)
        throw new TypeError("cannot look up something that isn't a string or array or strings")
    }