You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于类型键值补全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 addressOrgName to "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
  1. Modify DELIVERY Address: We use find() to locate the original DELIVERY address, then create a copy with the updated addressOrgName using the spread operator (...)—this avoids altering the original data, which is a good practice for immutability.
  2. 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).
  3. Flexible Scaling: If you ever need to add more address types later, you just update the requiredTypes array—no other code changes needed.

内容的提问来源于stack exchange,提问作者Mauro74

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:35:58