将两个 json 数组相交并组合所有字段,而不使用显式命名字段进行组合

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

我已经使用免费的在线工具将两个 Excel 工作表转换为 json。它为每个工作表创建两个单独的 json 属性。所以我们有以下 json 示例:

{
  "Product Info": [
    {
      "ID": 1,
      "Fuel Source": "Wood",
      "Type": "Freestanding",
      "Model": "F1150",
      "Catalytic": "No",
      "Maximum BTU's Per Hour": 55000,
      "Higher Heating Value Efficiency (HHV)": 0.7,
      "Lower Heating Value Efficiency (LHV)": 0.75,
      "Steady-State Efficiency": "n/a",
      "Link To Unit": "http://www.regency-fire.com/en/Products/Wood/Wood-Stoves/F1150",
      "Key Features": "Large fire viewing area, with heat radiating ceramic glass\nWood-finished handle\nHeavy duty cast hinges that will never bend or break\nSolid forged steel adjustable cam lock ensures a tight door seal over time\nFront mounted air adjustment\nAir operating safety tool\nHigh performance brick-lined firebox reflects heat\nNon-obtrusive air wash curtain keeps glass clean\nHeavy gauge steel log retainers to stop logs from rolling\nWelded ash lip will never bend or break\nRear heat deflector allowing for closer clearances\nMobile home and alcove approved\nDual-burn design creates longer and more complete burns\nBacked by the industry’s most comprehensive Limited Lifetime Warranty",
      "Product Size": "Small",
      "Maximum Log Size": "18\"",
      "Burn Time*": "Up to 8 hours",
      "Firebox Capacity": "1.3 cu. ft.",
      "Hopper Size": "n/a",
      "Emissions (g/hr)": "1.7 g/hr",
      "View Area": "126 sq. in.",
      "Exhaust Size": "6\"",
      "Minimum Fireplace Opening (W x H x D)": "n/a",
      "Fireplace Dimensions (W x H x D)": "24\" x 32-3/16\" x 20\"",
      "Picture Name": "F1150.jpg",
      "Feature Sheet File Name": "F1150 Feature Sheet.pdf",
      "Description": "The Regency Classic Small Wood Stove is perfect for smaller spaces, or for those needing to supplement their current heating system. It's compact footprint does not disappoint in heating capacity; burning efficiently, and extracting all the heat possible from one load of wood. Turn down your furnace and save money with this Regency small wood stove.",
      "Teaser": "A perfect fit for smaller rooms.  This small non-catalytic wood stove, comes with your choice of pedestal or legs and it is available in black or with nickel accents.",
      "Additional Picture Name #1": "F1150-B.jpg",
      "Pricing Reference Name": "001-915"
    },
    {
      "ID": 2,
      "Fuel Source": "Wood",
      "Type": "Freestanding",
      "Model": "F2450",
      "Catalytic": "No",
      "Maximum BTU's Per Hour": 75000,
      "Higher Heating Value Efficiency (HHV)": 0.733,
      "Lower Heating Value Efficiency (LHV)": 78.4,
      "Steady-State Efficiency": "n/a",
      "Link To Unit": "http://www.regency-fire.com/en/Products/Wood/Wood-Stoves/F2450",
      "Key Features": "Large fire viewing area, with heat radiating ceramic glass\nWood-finished handle\nHeavy duty cast hinges that will never bend or break\nSolid forged steel adjustable cam lock ensures a tight door seal over time\nFront mounted air adjustment\nAir operating safety tool\nHigh performance brick-lined firebox reflects heat\nNon-obtrusive air wash curtain keeps glass clean\nWelded ash lip will never bend or break\nRear heat deflector allowing for closer clearances\nMobile home and alcove approved\nDual-burn design creates longer and more complete burns\nBacked by the industry’s most comprehensive Limited Lifetime Warranty",
      "Product Size": "Medium",
      "Maximum Log Size": "18\"",
      "Burn Time*": "Up to 10 hours",
      "Firebox Capacity": "2.3 cu. ft.",
      "Hopper Size": "n/a",
      "Emissions (g/hr)": "2.3 g/hr",
      "View Area": "161 sq. in.",
      "Exhaust Size": "6\"",
      "Minimum Fireplace Opening (W x H x D)": "n/a",
      "Fireplace Dimensions (W x H x D)": "24\" x 36-5/16\" x 27-3/16\"",
      "Picture Name": "F2450.jpg",
      "Feature Sheet File Name": "F2450 Feature Sheet.pdf",
      "Description": "The Regency Classic Medium Wood Stove provides the perfect amount of heat for most spaces. Load wood in this stove front to back or side to side for the optimum convenience. Backed by our Limited Lifetime Warranty, we are confident that the quality workmanship, finest materials and durable construction of all Regency products, will serve your family for years to come.",
      "Teaser": "The F2450 is a Medium non-catalytic wood stove, comes with your choice of pedestal or legs and it is available in black or with nickel accents.",
      "Additional Picture Name #1": "F2450-B.jpg",
      "Additional Picture Name #2": "F2450-C.jpg",
      "Pricing Reference Name": "001-946"
    }
  ],
  "Company Pricing": [
    {
      "Part Num": "001-915",
      "Part Description": "Gravity Air Kit Z2510/FP90/CF780",
      "Prod Group": "08-WOOD",
      "MSPR USD": 975
    },
    {
      "Part Num": "001-946",
      "Part Description": "Adaptor Air Cool Chimney Sec FTF8",
      "Prod Group": "08-WOOD",
      "MSPR USD": 76
    }
  ]
}

这两个属性在以下获取请求中返回:

  const getProductInfo = async () => {
    isLoading.value = true;
    return  await useFetch(window.location.origin + '/assets/json/data.json', {
      onRequestError({ request, options, error }) {
        console.log(error);
        isLoading.value = false;
      },
      onResponse({ request, response, options }) {
        products.value = response._data;
        console.log(response._data)
        isLoading.value = false;
      },
      onResponseError({ request, response, options }) {
        console.log(request.error);
        isLoading.value = false;
      }
    });
  };

我想要完成的是将这两个属性合并到一个属性中,这样我就有一个干净的数组,其中包含产品信息的所有属性和公司定价中的定价。两者之间的关键是“定价参考名称”和“零件编号”。如果有匹配,我会做一个 for 循环并推送每个字段,但我认为必须有一种更有效的方法,并且不需要命名每个字段,因为这些字段可能会随着时间的推移而改变,但我希望这个函数返回所有内容。我不确定是否有办法用 ... 符号来实现所有字段?

无论哪种方式,我都无法在堆栈上找到具有此确切请求的示例,因此希望这是可行的。

javascript json vue.js fetch intersection
1个回答
0
投票

可能最简单的方法是根据

Product Info
Map
迭代为
Pricing Reference Name
,然后迭代
Company Pricing
,使用
Part Num
在该 Map 中查找适当的产品信息:

products = {
  "Product Info": [
    {
      "ID": 1,
      "Fuel Source": "Wood",
      "Type": "Freestanding",
      "Model": "F1150",
      "Catalytic": "No",
      "Maximum BTU's Per Hour": 55000,
      "Higher Heating Value Efficiency (HHV)": 0.7,
      "Lower Heating Value Efficiency (LHV)": 0.75,
      "Steady-State Efficiency": "n/a",
      "Link To Unit": "http://www.regency-fire.com/en/Products/Wood/Wood-Stoves/F1150",
      "Key Features": "Large fire viewing area, with heat radiating ceramic glass\nWood-finished handle\nHeavy duty cast hinges that will never bend or break\nSolid forged steel adjustable cam lock ensures a tight door seal over time\nFront mounted air adjustment\nAir operating safety tool\nHigh performance brick-lined firebox reflects heat\nNon-obtrusive air wash curtain keeps glass clean\nHeavy gauge steel log retainers to stop logs from rolling\nWelded ash lip will never bend or break\nRear heat deflector allowing for closer clearances\nMobile home and alcove approved\nDual-burn design creates longer and more complete burns\nBacked by the industry’s most comprehensive Limited Lifetime Warranty",
      "Product Size": "Small",
      "Maximum Log Size": "18\"",
      "Burn Time*": "Up to 8 hours",
      "Firebox Capacity": "1.3 cu. ft.",
      "Hopper Size": "n/a",
      "Emissions (g/hr)": "1.7 g/hr",
      "View Area": "126 sq. in.",
      "Exhaust Size": "6\"",
      "Minimum Fireplace Opening (W x H x D)": "n/a",
      "Fireplace Dimensions (W x H x D)": "24\" x 32-3/16\" x 20\"",
      "Picture Name": "F1150.jpg",
      "Feature Sheet File Name": "F1150 Feature Sheet.pdf",
      "Description": "The Regency Classic Small Wood Stove is perfect for smaller spaces, or for those needing to supplement their current heating system. It's compact footprint does not disappoint in heating capacity; burning efficiently, and extracting all the heat possible from one load of wood. Turn down your furnace and save money with this Regency small wood stove.",
      "Teaser": "A perfect fit for smaller rooms.  This small non-catalytic wood stove, comes with your choice of pedestal or legs and it is available in black or with nickel accents.",
      "Additional Picture Name #1": "F1150-B.jpg",
      "Pricing Reference Name": "001-915"
    },
    {
      "ID": 2,
      "Fuel Source": "Wood",
      "Type": "Freestanding",
      "Model": "F2450",
      "Catalytic": "No",
      "Maximum BTU's Per Hour": 75000,
      "Higher Heating Value Efficiency (HHV)": 0.733,
      "Lower Heating Value Efficiency (LHV)": 78.4,
      "Steady-State Efficiency": "n/a",
      "Link To Unit": "http://www.regency-fire.com/en/Products/Wood/Wood-Stoves/F2450",
      "Key Features": "Large fire viewing area, with heat radiating ceramic glass\nWood-finished handle\nHeavy duty cast hinges that will never bend or break\nSolid forged steel adjustable cam lock ensures a tight door seal over time\nFront mounted air adjustment\nAir operating safety tool\nHigh performance brick-lined firebox reflects heat\nNon-obtrusive air wash curtain keeps glass clean\nWelded ash lip will never bend or break\nRear heat deflector allowing for closer clearances\nMobile home and alcove approved\nDual-burn design creates longer and more complete burns\nBacked by the industry’s most comprehensive Limited Lifetime Warranty",
      "Product Size": "Medium",
      "Maximum Log Size": "18\"",
      "Burn Time*": "Up to 10 hours",
      "Firebox Capacity": "2.3 cu. ft.",
      "Hopper Size": "n/a",
      "Emissions (g/hr)": "2.3 g/hr",
      "View Area": "161 sq. in.",
      "Exhaust Size": "6\"",
      "Minimum Fireplace Opening (W x H x D)": "n/a",
      "Fireplace Dimensions (W x H x D)": "24\" x 36-5/16\" x 27-3/16\"",
      "Picture Name": "F2450.jpg",
      "Feature Sheet File Name": "F2450 Feature Sheet.pdf",
      "Description": "The Regency Classic Medium Wood Stove provides the perfect amount of heat for most spaces. Load wood in this stove front to back or side to side for the optimum convenience. Backed by our Limited Lifetime Warranty, we are confident that the quality workmanship, finest materials and durable construction of all Regency products, will serve your family for years to come.",
      "Teaser": "The F2450 is a Medium non-catalytic wood stove, comes with your choice of pedestal or legs and it is available in black or with nickel accents.",
      "Additional Picture Name #1": "F2450-B.jpg",
      "Additional Picture Name #2": "F2450-C.jpg",
      "Pricing Reference Name": "001-946"
    }
  ],
  "Company Pricing": [
    {
      "Part Num": "001-915",
      "Part Description": "Gravity Air Kit Z2510/FP90/CF780",
      "Prod Group": "08-WOOD",
      "MSPR USD": 975
    },
    {
      "Part Num": "001-946",
      "Part Description": "Adaptor Air Cool Chimney Sec FTF8",
      "Prod Group": "08-WOOD",
      "MSPR USD": 76
    }
  ]
}

info = new Map(products['Product Info'].map(o => [o['Pricing Reference Name'], o]))

result = products['Company Pricing'].map(o => ({ ...info.get(o['Part Num']), ...o }))

console.log(result)

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