1

I have an array of objects like this:

"addresses": [
    {
      "addressOrgName": "ACME",
      "addressLine1": "1 BRAIDWOOD AVENUE",
      "addressLine2": "KNUTSFORD",
      "county": "CHESHIRE",
      "postCode": "WA1 1QP",
      "country": "UNITED KINGDOM",
      "type": "DELIVERY",
      "telephoneNumber" : "0151234533"
    },
    {
      "addressOrgName": "ABC SUPPLIES",
      "addressLine1": "UNIT 4 MILLENNIUM BUSINESS ESTATE",
      "addressLine2": "BRUNTWOOD",
      "county": "DEVON",
      "postCode": "D1 5FG",
      "country": "UNITED KINGDOM",
      "type": "COLLECTION"
    },
    {
      "addressOrgName": "EFG ELECTRICAL",
      "addressLine1": "UNIT 4 MILLENNIUM BUSINESS ESTATE",
      "addressLine2": "BRUNTWOOD",
      "county": "DEVON",
      "postCode": "D1 5FG",
      "country": "UNITED KINGDOM",
      "type": "RETURN"
    }
  ]

One or two addresses type may not be present, but there will always be the one with type: DELIVERY. What I need to accomplish is to check if and which one are not there and push into the array the one(s) missing, so the resulting array will be like this:

"addresses": [
    {
      "addressOrgName": "ADDRESSEE ONLY",
      "addressLine1": "1 BRAIDWOOD AVENUE",
      "addressLine2": "KNUTSFORD",
      "county": "CHESHIRE",
      "postCode": "WA1 1QP",
      "country": "UNITED KINGDOM",
      "type": "DELIVERY",
      "telephoneNumber" : "0151234533"
    },
    {
      "addressOrgName": "",
      "addressLine1": "",
      "addressLine2": "",
      "county": "",
      "postCode": "",
      "country": "",
      "type": "COLLECTION"
    },
    {
      "addressOrgName": "",
      "addressLine1": "",
      "addressLine2": "",
      "county": "",
      "postCode": "",
      "country": "",
      "type": "RETURN"
    }
  ]

No idea how to approach it. Ideas?

Thank you

Mauro74
  • 4,686
  • 15
  • 58
  • 80

4 Answers4

1

Use find, and if the addresses types are not found, push them :

let addresses = [{
    "addressOrgName": "ADDRESSEE ONLY",
    "addressLine1": "1 BRAIDWOOD AVENUE",
    "addressLine2": "KNUTSFORD",
    "county": "CHESHIRE",
    "postCode": "WA1 1QP",
    "country": "UNITED KINGDOM",
    "type": "DELIVERY",
    "telephoneNumber": "0151234533"
  }],
  emptyAddress = {
    "addressOrgName": "",
    "addressLine1": "",
    "addressLine2": "",
    "county": "",
    "postCode": "",
    "country": "",
    "type": ""
  };

for(let type of ["COLLECTION", "RETURN"]) {
  if (!addresses.find(a => a.type === type)) addresses.push(Object.assign(emptyAddress, {type}))
}

console.log(addresses)
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
1

Iterate over each needed type and add it to the array if it's not found:

const addresses = [{
  "addressOrgName": "EFG ELECTRICAL",
  "addressLine1": "UNIT 4 MILLENNIUM BUSINESS ESTATE",
  "addressLine2": "BRUNTWOOD",
  "county": "DEVON",
  "postCode": "D1 5FG",
  "country": "UNITED KINGDOM",
  "type": "RETURN"
}
                  ];
const addTypes = ['DELIVERY', 'COLLECTION'];
addTypes.forEach((addType) => {
  const foundObj = addresses.find(({ type }) => type === addType);
  if (foundObj) return;
  addresses.push({
    "addressOrgName": "",
    "addressLine1": "",
    "addressLine2": "",
    "county": "",
    "postCode": "",
    "country": "",
    "type": addType,
  });
});
console.log(addresses);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I know this technically produces a correct-ish result (op specified all items will have delivery), but I think it can be improved. It assumes you already know what is contained in the list (easily changed by adding `RETURN` to the array). It also couples the act of finding missing items, and appending blanks. This is the type of code that works, but will cause headaches later on, trying to understand why it does what it does. – Matt Way May 09 '18 at 10:36
0

Just loop your array and check object type, if type is DELIVERY, push this object to a new array.

Example:

var addresses = [
  {
    "addressOrgName": "ACME",
    "addressLine1": "1 BRAIDWOOD AVENUE",
    "addressLine2": "KNUTSFORD",
    "county": "CHESHIRE",
    "postCode": "WA1 1QP",
    "country": "UNITED KINGDOM",
    "type": "DELIVERY",
    "telephoneNumber" : "0151234533"
  },
  {
    "addressOrgName": "ABC SUPPLIES",
    "addressLine1": "UNIT 4 MILLENNIUM BUSINESS ESTATE",
    "addressLine2": "BRUNTWOOD",
    "county": "DEVON",
    "postCode": "D1 5FG",
    "country": "UNITED KINGDOM",
    "type": "COLLECTION"
  },
  {
    "addressOrgName": "EFG ELECTRICAL",
    "addressLine1": "UNIT 4 MILLENNIUM BUSINESS ESTATE",
    "addressLine2": "BRUNTWOOD",
    "county": "DEVON",
    "postCode": "D1 5FG",
    "country": "UNITED KINGDOM",
    "type": "RETURN"
  }
]

var newAddresses = [];

for (var i = 0; i < addresses.length; i++) {
  if (addresses[i].type === 'DELIVERY') {
    newAddresses.push(addresses[i]);
  } else {
    newAddresses.push({
      "addressOrgName": "",
      "addressLine1": "",
      "addressLine2": "",
      "county": "",
      "postCode": "",
      "country": "",
      "type": addresses[i].type
    })
  }
}

addresses = newAddresses;

console.log(addresses);
Huy Chau
  • 2,178
  • 19
  • 26
0

If I am reading your question correctly, and you actually want blank information (except for the type), in the added items, you could do something like this (Please note this modifies the original array. It shouldn't be hard to change if you want a new array though).

The way this works is that I build a list of the found types, and do an array difference with the required types. This leaves the types that need to be added. I like this way because it clearly separates figuring out which types are missing, and what to do with them. This way if you want to modify later it should be easier.

I also think it is better to include DELIVERY in the required types, and not hardcode it, as this requirement might also change in the future. The only way to make it more general would be to allow an optional function that returns the key to check.

var addresses = [
  {
    "addressOrgName": "ACME",
    "addressLine1": "1 BRAIDWOOD AVENUE",
    "addressLine2": "KNUTSFORD",
    "county": "CHESHIRE",
    "postCode": "WA1 1QP",
    "country": "UNITED KINGDOM",
    "type": "DELIVERY",
    "telephoneNumber" : "0151234533"
  }
]

const getBlankType = type => ({
  addressOrgName: "",
  addressLine1: "",
  addressLine2: "",
  county: "",
  postCode: "",
  country: "",
  type: type
})

const getMissing = (arr, requiredTypes) => {
  // get the known types
  const has = arr.map(i => i.type)
  // get the array difference with the required types
  return requiredTypes.filter(t => has.indexOf(t) < 0)
}

const missing = getMissing(addresses, ['DELIVERY', 'COLLECTION', 'RETURN'])
// add the missing blank types
missing.forEach(type => addresses.push(getBlankType(type)))
console.log(addresses)
Matt Way
  • 32,319
  • 10
  • 79
  • 85