需要在JS中从JSON获取特定的属性/值

问题描述 投票:0回答:2

我有一个 JSON,我需要获取 SKU 属性值作为结果数组。 SKU 属性将位于 shipping 和 OFSUC 数组内。因此,我尝试使用 JS 中的 filtermap 函数有效地获取这些值,但我不知道如何迭代多个数组。

我的 JSON 对象

{
"id": 103,
"consignments": [
    {
        "pickups": [],
        "shipping": [
            {
                "id": 4,
                "first_name": "Sabareesh",
                "line_items": [
                    {
                        "id": 5,
                        "order_id": 103,
                        "product_id": 86,
                        "variant_id": 66,
                        "order_pickup_method_id": 0,
                        "order_address_id": 4,
                        "name": "[Sample] Able Brewing System",
                        "name_customer": "[Sample] Able Brewing System",
                        "name_merchant": "[Sample] Able Brewing System",
                        "sku": "ABS",
                        "upc": "",
                        "type": "physical",
                        "base_price": "225.0000",
                        "price_ex_tax": "225.0000",
                        "price_inc_tax": "270.0000",
                        "price_tax": "45.0000",
                        "base_total": "225.0000",
                        "total_ex_tax": "225.0000",
                        "total_inc_tax": "270.0000",
                        "total_tax": "45.0000",
                        "weight": "1.0000",
                        "width": "10.0000",
                        "height": "10.0000",
                        "depth": "10.0000",
                        "quantity": 1,
                        "base_cost_price": "0.0000",
                        "cost_price_inc_tax": "0.0000",
                        "cost_price_ex_tax": "0.0000",
                        "cost_price_tax": "0.0000",
                        "is_refunded": false,
                        "quantity_refunded": 0,
                        "refund_amount": "0.0000",
                        "return_id": 0,
                        "wrapping_id": 0,
                        "wrapping_name": "",
                        "base_wrapping_cost": "0.0000",
                        "wrapping_cost_ex_tax": "0.0000",
                        "wrapping_cost_inc_tax": "0.0000",
                        "wrapping_cost_tax": "0.0000",
                        "wrapping_message": "",
                        "quantity_shipped": 0,
                        "event_name": null,
                        "event_date": "",
                        "fixed_shipping_cost": "0.0000",
                        "ebay_item_id": "",
                        "ebay_transaction_id": "",
                        "option_set_id": null,
                        "parent_order_product_id": null,
                        "is_bundled_product": false,
                        "bin_picking_number": "",
                        "external_id": null,
                        "fulfillment_source": "",
                        "brand": "",
                        "gift_certificate_id": null,
                        "applied_discounts": [],
                        "product_options": [],
                        "configurable_fields": [],
                        "discounted_total_inc_tax": "270.0000"
                    },
                    {
                        "id": 14,
                        "order_id": 103,
                        "product_id": 94,
                        "variant_id": 68,
                        "order_pickup_method_id": 0,
                        "order_address_id": 4,
                        "name": "[Sample] Oak Cheese Grater",
                        "name_customer": "[Sample] Oak Cheese Grater",
                        "name_merchant": "[Sample] Oak Cheese Grater",
                        "sku": "OCG",
                        "upc": "",
                        "type": "physical",
                        "base_price": "31.4600",
                        "price_ex_tax": "31.4600",
                        "price_inc_tax": "37.7500",
                        "price_tax": "6.2900",
                        "base_total": "31.4600",
                        "total_ex_tax": "31.4600",
                        "total_inc_tax": "37.7500",
                        "total_tax": "6.2900",
                        "weight": "1.0000",
                        "width": "0.0000",
                        "height": "0.0000",
                        "depth": "0.0000",
                        "quantity": 1,
                        "base_cost_price": "0.0000",
                        "cost_price_inc_tax": "0.0000",
                        "cost_price_ex_tax": "0.0000",
                        "cost_price_tax": "0.0000",
                        "is_refunded": false,
                        "quantity_refunded": 0,
                        "refund_amount": "0.0000",
                        "return_id": 0,
                        "wrapping_id": 0,
                        "wrapping_name": "",
                        "base_wrapping_cost": "0.0000",
                        "wrapping_cost_ex_tax": "0.0000",
                        "wrapping_cost_inc_tax": "0.0000",
                        "wrapping_cost_tax": "0.0000",
                        "wrapping_message": "",
                        "quantity_shipped": 0,
                        "event_name": null,
                        "event_date": "",
                        "fixed_shipping_cost": "0.0000",
                        "ebay_item_id": "",
                        "ebay_transaction_id": "",
                        "option_set_id": null,
                        "parent_order_product_id": null,
                        "is_bundled_product": false,
                        "bin_picking_number": "",
                        "external_id": null,
                        "fulfillment_source": "",
                        "brand": "Sagaform",
                        "gift_certificate_id": null,
                        "applied_discounts": [],
                        "product_options": [],
                        "configurable_fields": [],
                        "discounted_total_inc_tax": "37.7500"
                    }
                ],
                "items_total": 2,
                "items_shipped": 0,
                "shipping_method": "Free Shipping",
                "base_cost": 0,
                "cost_ex_tax": 0,
                "cost_inc_tax": 0,
                "cost_tax": 0,
                "cost_tax_class_id": 2,
                "base_handling_cost": 0,
                "handling_cost_ex_tax": 0,
                "handling_cost_inc_tax": 0,
                "handling_cost_tax": 0,
                "handling_cost_tax_class_id": 2,
                "shipping_zone_id": 2,
                "shipping_zone_name": "Qatar"      
            }
        ],
        "downloads": [
            {
                "line_items": [
                    {
                        "id": 15,
                        "order_id": 103,
                        "product_id": 104,
                        "variant_id": 72,
                        "order_pickup_method_id": 0,
                        "order_address_id": 0,
                        "name": "[Sample] Utility Caddy",
                        "name_customer": "[Sample] Utility Caddy",
                        "name_merchant": "[Sample] Utility Caddy",
                        "sku": "OFSUC",
                        "upc": "",
                        "type": "digital",
                        "base_price": "41.3600",
                        "price_ex_tax": "41.3600",
                        "price_inc_tax": "49.6300",
                        "price_tax": "8.2700",
                        "base_total": "41.3600",
                        "total_ex_tax": "41.3600",
                        "total_inc_tax": "49.6300",
                        "total_tax": "8.2700",
                        "weight": "0.0000",
                        "width": "0.0000",
                        "height": "0.0000",
                        "depth": "0.0000",
                        "quantity": 1,
                        "base_cost_price": "0.0000",
                        "cost_price_inc_tax": "0.0000",
                        "cost_price_ex_tax": "0.0000",
                        "cost_price_tax": "0.0000",
                        "is_refunded": false,
                        "quantity_refunded": 0,
                        "refund_amount": "0.0000",
                        "return_id": 0,
                        "wrapping_id": 0,
                        "wrapping_name": "",
                        "base_wrapping_cost": "0.0000",
                        "wrapping_cost_ex_tax": "0.0000",
                        "wrapping_cost_inc_tax": "0.0000",
                        "wrapping_cost_tax": "0.0000",
                        "wrapping_message": "",
                        "quantity_shipped": 0,
                        "event_name": null,
                        "event_date": "",
                        "fixed_shipping_cost": "0.0000",
                        "ebay_item_id": "",
                        "ebay_transaction_id": "",
                        "option_set_id": null,
                        "parent_order_product_id": null,
                        "is_bundled_product": false,
                        "bin_picking_number": "",
                        "external_id": null,
                        "fulfillment_source": "",
                        "brand": "OFS",
                        "gift_certificate_id": null,
                        "applied_discounts": [],
                        "product_options": [],
                        "configurable_fields": [],
                        "discounted_total_inc_tax": "49.6300"
                    }
                ]
            }
        ],
        "email": {
            "gift_certificates": []
        }
    }
]

}

预期结果: [“OCG”、“ABS”、“OFSUC”]

javascript arrays json filter nestjs
2个回答
0
投票

您可以尝试以下方法:

const jsonObject={id:103,consignments:[{pickups:[],shipping:[{id:4,first_name:"Sabareesh",line_items:[{id:5,order_id:103,product_id:86,variant_id:66,order_pickup_method_id:0,order_address_id:4,name:"[Sample] Able Brewing System",name_customer:"[Sample] Able Brewing System",name_merchant:"[Sample] Able Brewing System",sku:"ABS",upc:"",type:"physical",base_price:"225.0000",price_ex_tax:"225.0000",price_inc_tax:"270.0000",price_tax:"45.0000",base_total:"225.0000",total_ex_tax:"225.0000",total_inc_tax:"270.0000",total_tax:"45.0000",weight:"1.0000",width:"10.0000",height:"10.0000",depth:"10.0000",quantity:1,base_cost_price:"0.0000",cost_price_inc_tax:"0.0000",cost_price_ex_tax:"0.0000",cost_price_tax:"0.0000",is_refunded:!1,quantity_refunded:0,refund_amount:"0.0000",return_id:0,wrapping_id:0,wrapping_name:"",base_wrapping_cost:"0.0000",wrapping_cost_ex_tax:"0.0000",wrapping_cost_inc_tax:"0.0000",wrapping_cost_tax:"0.0000",wrapping_message:"",quantity_shipped:0,event_name:null,event_date:"",fixed_shipping_cost:"0.0000",ebay_item_id:"",ebay_transaction_id:"",option_set_id:null,parent_order_product_id:null,is_bundled_product:!1,bin_picking_number:"",external_id:null,fulfillment_source:"",brand:"",gift_certificate_id:null,applied_discounts:[],product_options:[],configurable_fields:[],discounted_total_inc_tax:"270.0000"},{id:14,order_id:103,product_id:94,variant_id:68,order_pickup_method_id:0,order_address_id:4,name:"[Sample] Oak Cheese Grater",name_customer:"[Sample] Oak Cheese Grater",name_merchant:"[Sample] Oak Cheese Grater",sku:"OCG",upc:"",type:"physical",base_price:"31.4600",price_ex_tax:"31.4600",price_inc_tax:"37.7500",price_tax:"6.2900",base_total:"31.4600",total_ex_tax:"31.4600",total_inc_tax:"37.7500",total_tax:"6.2900",weight:"1.0000",width:"0.0000",height:"0.0000",depth:"0.0000",quantity:1,base_cost_price:"0.0000",cost_price_inc_tax:"0.0000",cost_price_ex_tax:"0.0000",cost_price_tax:"0.0000",is_refunded:!1,quantity_refunded:0,refund_amount:"0.0000",return_id:0,wrapping_id:0,wrapping_name:"",base_wrapping_cost:"0.0000",wrapping_cost_ex_tax:"0.0000",wrapping_cost_inc_tax:"0.0000",wrapping_cost_tax:"0.0000",wrapping_message:"",quantity_shipped:0,event_name:null,event_date:"",fixed_shipping_cost:"0.0000",ebay_item_id:"",ebay_transaction_id:"",option_set_id:null,parent_order_product_id:null,is_bundled_product:!1,bin_picking_number:"",external_id:null,fulfillment_source:"",brand:"Sagaform",gift_certificate_id:null,applied_discounts:[],product_options:[],configurable_fields:[],discounted_total_inc_tax:"37.7500"}],items_total:2,items_shipped:0,shipping_method:"Free Shipping",base_cost:0,cost_ex_tax:0,cost_inc_tax:0,cost_tax:0,cost_tax_class_id:2,base_handling_cost:0,handling_cost_ex_tax:0,handling_cost_inc_tax:0,handling_cost_tax:0,handling_cost_tax_class_id:2,shipping_zone_id:2,shipping_zone_name:"Qatar"}],downloads:[{line_items:[{id:15,order_id:103,product_id:104,variant_id:72,order_pickup_method_id:0,order_address_id:0,name:"[Sample] Utility Caddy",name_customer:"[Sample] Utility Caddy",name_merchant:"[Sample] Utility Caddy",sku:"OFSUC",upc:"",type:"digital",base_price:"41.3600",price_ex_tax:"41.3600",price_inc_tax:"49.6300",price_tax:"8.2700",base_total:"41.3600",total_ex_tax:"41.3600",total_inc_tax:"49.6300",total_tax:"8.2700",weight:"0.0000",width:"0.0000",height:"0.0000",depth:"0.0000",quantity:1,base_cost_price:"0.0000",cost_price_inc_tax:"0.0000",cost_price_ex_tax:"0.0000",cost_price_tax:"0.0000",is_refunded:!1,quantity_refunded:0,refund_amount:"0.0000",return_id:0,wrapping_id:0,wrapping_name:"",base_wrapping_cost:"0.0000",wrapping_cost_ex_tax:"0.0000",wrapping_cost_inc_tax:"0.0000",wrapping_cost_tax:"0.0000",wrapping_message:"",quantity_shipped:0,event_name:null,event_date:"",fixed_shipping_cost:"0.0000",ebay_item_id:"",ebay_transaction_id:"",option_set_id:null,parent_order_product_id:null,is_bundled_product:!1,bin_picking_number:"",external_id:null,fulfillment_source:"",brand:"OFS",gift_certificate_id:null,applied_discounts:[],product_options:[],configurable_fields:[],discounted_total_inc_tax:"49.6300"}]}],email:{gift_certificates:[]}}]};


//use flatMap to iterate over consignments array
const skuArray = jsonObject.consignments.flatMap((consignment) =>
  //concatenate sku values from shipping and downloads arrays
  consignment.shipping.concat(consignment.downloads)
    //flatten the nested arrays and extract sku values
    .flatMap((item) => item.line_items.flatMap((subItem) => subItem.sku))
);


console.log(skuArray);


0
投票

如果您需要提取给定对象中任何深处的属性

sku
的所有值,您可以递归地迭代对象的属性并返回映射到它的值:

function findAllSKUs(obj) {
    let skus = [];

    function search(obj) {
        if (obj !== null && typeof obj === 'object') {
            Object.entries(obj).forEach(([key, value]) => {
                if (key === 'sku' && typeof value === 'string') {
                    skus.push(value);
                } else {
                    search(value);
                }
            });
        }
    }

    search(obj);
    return skus;
}

const data =
{
"id": 103,
"consignments": [
    {
        "pickups": [],
        "shipping": [
            {
                "id": 4,
                "first_name": "Sabareesh",
                "line_items": [
                    {
                        "id": 5,
                        "order_id": 103,
                        "product_id": 86,
                        "variant_id": 66,
                        "order_pickup_method_id": 0,
                        "order_address_id": 4,
                        "name": "[Sample] Able Brewing System",
                        "name_customer": "[Sample] Able Brewing System",
                        "name_merchant": "[Sample] Able Brewing System",
                        "sku": "ABS",
                        "upc": "",
                        "type": "physical",
                        "base_price": "225.0000",
                        "price_ex_tax": "225.0000",
                        "price_inc_tax": "270.0000",
                        "price_tax": "45.0000",
                        "base_total": "225.0000",
                        "total_ex_tax": "225.0000",
                        "total_inc_tax": "270.0000",
                        "total_tax": "45.0000",
                        "weight": "1.0000",
                        "width": "10.0000",
                        "height": "10.0000",
                        "depth": "10.0000",
                        "quantity": 1,
                        "base_cost_price": "0.0000",
                        "cost_price_inc_tax": "0.0000",
                        "cost_price_ex_tax": "0.0000",
                        "cost_price_tax": "0.0000",
                        "is_refunded": false,
                        "quantity_refunded": 0,
                        "refund_amount": "0.0000",
                        "return_id": 0,
                        "wrapping_id": 0,
                        "wrapping_name": "",
                        "base_wrapping_cost": "0.0000",
                        "wrapping_cost_ex_tax": "0.0000",
                        "wrapping_cost_inc_tax": "0.0000",
                        "wrapping_cost_tax": "0.0000",
                        "wrapping_message": "",
                        "quantity_shipped": 0,
                        "event_name": null,
                        "event_date": "",
                        "fixed_shipping_cost": "0.0000",
                        "ebay_item_id": "",
                        "ebay_transaction_id": "",
                        "option_set_id": null,
                        "parent_order_product_id": null,
                        "is_bundled_product": false,
                        "bin_picking_number": "",
                        "external_id": null,
                        "fulfillment_source": "",
                        "brand": "",
                        "gift_certificate_id": null,
                        "applied_discounts": [],
                        "product_options": [],
                        "configurable_fields": [],
                        "discounted_total_inc_tax": "270.0000"
                    },
                    {
                        "id": 14,
                        "order_id": 103,
                        "product_id": 94,
                        "variant_id": 68,
                        "order_pickup_method_id": 0,
                        "order_address_id": 4,
                        "name": "[Sample] Oak Cheese Grater",
                        "name_customer": "[Sample] Oak Cheese Grater",
                        "name_merchant": "[Sample] Oak Cheese Grater",
                        "sku": "OCG",
                        "upc": "",
                        "type": "physical",
                        "base_price": "31.4600",
                        "price_ex_tax": "31.4600",
                        "price_inc_tax": "37.7500",
                        "price_tax": "6.2900",
                        "base_total": "31.4600",
                        "total_ex_tax": "31.4600",
                        "total_inc_tax": "37.7500",
                        "total_tax": "6.2900",
                        "weight": "1.0000",
                        "width": "0.0000",
                        "height": "0.0000",
                        "depth": "0.0000",
                        "quantity": 1,
                        "base_cost_price": "0.0000",
                        "cost_price_inc_tax": "0.0000",
                        "cost_price_ex_tax": "0.0000",
                        "cost_price_tax": "0.0000",
                        "is_refunded": false,
                        "quantity_refunded": 0,
                        "refund_amount": "0.0000",
                        "return_id": 0,
                        "wrapping_id": 0,
                        "wrapping_name": "",
                        "base_wrapping_cost": "0.0000",
                        "wrapping_cost_ex_tax": "0.0000",
                        "wrapping_cost_inc_tax": "0.0000",
                        "wrapping_cost_tax": "0.0000",
                        "wrapping_message": "",
                        "quantity_shipped": 0,
                        "event_name": null,
                        "event_date": "",
                        "fixed_shipping_cost": "0.0000",
                        "ebay_item_id": "",
                        "ebay_transaction_id": "",
                        "option_set_id": null,
                        "parent_order_product_id": null,
                        "is_bundled_product": false,
                        "bin_picking_number": "",
                        "external_id": null,
                        "fulfillment_source": "",
                        "brand": "Sagaform",
                        "gift_certificate_id": null,
                        "applied_discounts": [],
                        "product_options": [],
                        "configurable_fields": [],
                        "discounted_total_inc_tax": "37.7500"
                    }
                ],
                "items_total": 2,
                "items_shipped": 0,
                "shipping_method": "Free Shipping",
                "base_cost": 0,
                "cost_ex_tax": 0,
                "cost_inc_tax": 0,
                "cost_tax": 0,
                "cost_tax_class_id": 2,
                "base_handling_cost": 0,
                "handling_cost_ex_tax": 0,
                "handling_cost_inc_tax": 0,
                "handling_cost_tax": 0,
                "handling_cost_tax_class_id": 2,
                "shipping_zone_id": 2,
                "shipping_zone_name": "Qatar"      
            }
        ],
        "downloads": [
            {
                "line_items": [
                    {
                        "id": 15,
                        "order_id": 103,
                        "product_id": 104,
                        "variant_id": 72,
                        "order_pickup_method_id": 0,
                        "order_address_id": 0,
                        "name": "[Sample] Utility Caddy",
                        "name_customer": "[Sample] Utility Caddy",
                        "name_merchant": "[Sample] Utility Caddy",
                        "sku": "OFSUC",
                        "upc": "",
                        "type": "digital",
                        "base_price": "41.3600",
                        "price_ex_tax": "41.3600",
                        "price_inc_tax": "49.6300",
                        "price_tax": "8.2700",
                        "base_total": "41.3600",
                        "total_ex_tax": "41.3600",
                        "total_inc_tax": "49.6300",
                        "total_tax": "8.2700",
                        "weight": "0.0000",
                        "width": "0.0000",
                        "height": "0.0000",
                        "depth": "0.0000",
                        "quantity": 1,
                        "base_cost_price": "0.0000",
                        "cost_price_inc_tax": "0.0000",
                        "cost_price_ex_tax": "0.0000",
                        "cost_price_tax": "0.0000",
                        "is_refunded": false,
                        "quantity_refunded": 0,
                        "refund_amount": "0.0000",
                        "return_id": 0,
                        "wrapping_id": 0,
                        "wrapping_name": "",
                        "base_wrapping_cost": "0.0000",
                        "wrapping_cost_ex_tax": "0.0000",
                        "wrapping_cost_inc_tax": "0.0000",
                        "wrapping_cost_tax": "0.0000",
                        "wrapping_message": "",
                        "quantity_shipped": 0,
                        "event_name": null,
                        "event_date": "",
                        "fixed_shipping_cost": "0.0000",
                        "ebay_item_id": "",
                        "ebay_transaction_id": "",
                        "option_set_id": null,
                        "parent_order_product_id": null,
                        "is_bundled_product": false,
                        "bin_picking_number": "",
                        "external_id": null,
                        "fulfillment_source": "",
                        "brand": "OFS",
                        "gift_certificate_id": null,
                        "applied_discounts": [],
                        "product_options": [],
                        "configurable_fields": [],
                        "discounted_total_inc_tax": "49.6300"
                    }
                ]
            }
        ],
        "email": {
            "gift_certificates": []
        }
    }
]};

const skus = findAllSKUs(data);
console.log(skus);

© www.soinside.com 2019 - 2024. All rights reserved.