Javascript根据其长度和键值修改数组

时间:2018-05-09 09:58:34

标签: javascript arrays

我有一个像这样的对象数组:

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

可能不存在一个或两个地址类型,但始终会有type: DELIVERY的地址类型。我需要做的是检查是否以及哪一个不存在并将数据丢失到数组中,因此得到的数组将是这样的:

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

不知道如何处理它。想法?

谢谢

4 个答案:

答案 0 :(得分:1)

使用find,如果找不到地址类型,请按下它们:

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)

答案 1 :(得分:1)

迭代每个需要的类型并将其添加到数组中(如果找不到它):



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);




答案 2 :(得分:0)

只需循环数组并检查对象类型,如果类型为 DELIVERY ,则将此对象推送到新数组。

示例:

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);

答案 3 :(得分:0)

如果我正确地阅读你的问题,而你实际上想要空白信息(除了类型),在添加的项目中,你可以做这样的事情(请注意这会修改原始数组。它不应该&#t; t如果你想要一个新阵列,很难改变。)

这样做的方法是我构建一个已找到类型的列表,并使用所需类型执行array difference。这留下了需要添加的类型。我喜欢这种方式,因为它清楚地分清了哪些类型缺失,以及如何处理它们。这样,如果您想稍后修改它应该更容易。

我还认为最好在所需类型中包含DELIVERY,而不是对其进行硬编码,因为此要求可能在将来也会发生变化。使其更通用的唯一方法是允许返回要检查的键的可选函数。

&#13;
&#13;
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)
&#13;
&#13;
&#13;