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"]

Dani AI

Generated

A practical, robust pattern is to prebuild a lookup of area → postcode(s) (normalize by trimming and lowercasing), then do O(1) lookups for the single string and each array item. That turns ’s loop idea into a one-pass precompute + cheap lookups, and directly addresses ’s need to keep singleAddress vs multipleAddresses distinct.

// build a map of normalized area -> array of postcodes
const buildLookup = records => {
  const map = new Map();
  for (const { postcode, area } of records) {
    area.split(',').forEach(name => {
      const key = name.trim().toLowerCase();
      if (!map.has(key)) map.set(key, []);
      map.get(key).push(postcode);
    });
  }
  return map;
};

const appendPostcode = (addr, lookup) => {
  const key = addr.trim().toLowerCase();
  const codes = lookup.get(key) || [];
  if (!codes.length) return addr;                     // no match -> unchanged
  const chosen = codes.length === 1 ? codes[0] : codes.join('/'); // handle duplicates
  return `${addr}, ${chosen}`;
};

// usage: build once, then map/look up
const lookup = buildLookup(plotData);
const updatedSingle = appendPostcode(singleAddress, lookup);
const updatedMultiple = multipleAddresses.map(a => appendPostcode(a, lookup));

Notes and gotchas: normalize consistently (trim, lowercase); split on the exact separator you use (commas here); decide how to handle duplicate matches (the snippet joins with “/” but you can pick first or prefer a rule). This is O(R * A + Q) work (records × areas per record, plus queries), so it’s efficient when you do many lookups. If you need fuzzy/substring matching instead of exact area names, use explicit regex checks or a fuzzy library but take care to avoid false positives.

convert this to actual code
func(address)
loop object
addr in object[i].area ?
then addr += object[i].postcode
else continue
end

loop for arrays
or make it recursive

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.