如何基于类型键值补全JavaScript地址对象数组?
Alright, let's solve this address array handling problem. Here's a clean, maintainable approach using JavaScript that covers all your requirements:
Solution Approach
First, let's break down what we need to accomplish:
- Ensure the final array always includes DELIVERY, COLLECTION, and RETURN address types
- For the DELIVERY address: retain all original field values but update
addressOrgNameto"ADDRESSEE ONLY" - For missing COLLECTION/RETURN types: add an empty object with all fields set to empty strings and the correct
type
Code Implementation
function processAddresses(originalAddresses) { // List all address types we need to ensure are present const requiredTypes = ['DELIVERY', 'COLLECTION', 'RETURN']; // Grab the original DELIVERY address (guaranteed to exist per requirements) const originalDelivery = originalAddresses.find(addr => addr.type === 'DELIVERY'); // Create a modified version with the updated org name (avoids mutating original) const modifiedDelivery = { ...originalDelivery, addressOrgName: 'ADDRESSEE ONLY' }; // Build our final addresses array const finalAddresses = requiredTypes.map(type => { if (type === 'DELIVERY') { return modifiedDelivery; } // Check if this type already exists in the original array const existingAddress = originalAddresses.find(addr => addr.type === type); if (existingAddress) { return existingAddress; // Keep the existing entry if present } // Return an empty object for missing types, matching the full field structure return { addressOrgName: '', addressLine1: '', addressLine2: '', county: '', postCode: '', country: '', type: type, telephoneNumber: '' // Include this to match the sample structure }; }); return finalAddresses; } // Example usage with your sample data const originalAddresses = [ { "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" } ]; const processedAddresses = processAddresses(originalAddresses); console.log(JSON.stringify({ addresses: processedAddresses }, null, 2));
How This Works
- Modify DELIVERY Address: We use
find()to locate the original DELIVERY address, then create a copy with the updatedaddressOrgNameusing the spread operator (...)—this avoids altering the original data, which is a good practice for immutability. - Validate Required Types: We loop through all three mandatory address types. For each type:
- If it's DELIVERY, we use our pre-modified object.
- If the type already exists in the input array, we keep that existing entry as-is.
- If the type is missing, we generate an empty object with all fields set to empty strings, matching the full structure of your sample data (including
telephoneNumber).
- Flexible Scaling: If you ever need to add more address types later, you just update the
requiredTypesarray—no other code changes needed.
内容的提问来源于stack exchange,提问作者Mauro74
相关产品推荐
相关产品推荐

