简体   繁体   English

JS - 在“减少”之后向子元素添加父值

[英]JS - Adding a parent value to a child element, after 'reduce'

I have created the srcript below that groups up 'productVariation' by 'variationName' from each of the elements in the array.我创建了下面的脚本,该脚本将数组中每个元素的“productVariation”按“variationName”分组。

However I am now trying to figure out how to add the 'vendor_uLID' to the 'variationOption', in a new element called: 'linkedVendors'.但是,我现在正试图弄清楚如何在名为“linkedVendors”的新元素中将“vendor_uLID”添加到“variationOption”中。

Below is the script I have built so far.以下是我迄今为止构建的脚本。

How do I add the relevant 'vendor_uLID' of the parent to the 'variationOption' as per the examples below?如何按照以下示例将父级的相关“vendor_uLID”添加到“variationOption”? (I have managed to add one, but can't add anymore) (我已经添加了一个,但不能再添加了)

Javascript Attempt: Javascript 尝试:

const output = arrayIn.reduce((currentOutput, company) => {
  company.productVariations.forEach(variation => {
    const variationIndex = currentOutput.findIndex(
      outputVariation =>
        variation.variationName === outputVariation.variationName
    )
    currentOutput.forEach(option => {
      option.variationOptions.forEach(op => {
        op['linkedVendors'] = []
        op.linkedVendors.push(company.vendor_uLID)
      })
    })
    if (variationIndex === -1) {
      currentOutput.push({
        variationName: variation.variationName,
        variationOptions: variation.variationOptions,
        record_uLID: variation.record_uLID,
      })
    } else {
      variation.variationOptions.forEach(variationOption => {
        if (
          !currentOutput[variationIndex].variationOptions.some(
            outputVariationOption =>
              variationOption.record_uLID === outputVariationOption.record_uLID
          )
        ) {
          currentOutput[variationIndex].variationOptions.push(variationOption)
        }
      })
    }
  })
  return currentOutput
}, [])

console.log(output)

Input Array:输入数组:

var arrayIn = [
  {
    vendor_uLID: '5e793a0411d2bef2e375cd00',
    productVariations: [
      {
        variationName: 'Colour',
        variationOptions: [
          {
            name: 'Blue',
            record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0051',
          },
          {
            name: 'yellow',
            record_uLID: '66654830-6850-490a-8eaf-9d505e3e4672',
          }
        ],
      },
      {
        variationName: 'Pattern',
        variationOptions: [
          {
            name: 'Bold',
            record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0055',
          },
          {
            name: 'Spotted',
            record_uLID: '66654830-6850-490a-8eaf-9d505e3e4671',
          },
          {
            name: 'Stripped',
            record_uLID: 'ec9b5fbe-6428-4a67-aab8-9a23cdce2f9f',
          },
        ],
      },
    ],
  },
  {
    vendor_uLID: '5e7bb266071f9601b6ad8f4e',
    productVariations: [
      {
        variationName: 'Colour',
        variationOptions: [
          {
            name: 'Blue',
            record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0051',
          },
          {
            name: 'yellow',
            record_uLID: '66654830-6850-490a-8eaf-9d505e3e4672',
          }
        ],
      },
      {
        variationName: 'Pattern',
        variationOptions: [
          {
            name: 'Bold',
            record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0055',
          },
          {
            name: 'Spotted',
            record_uLID: '66654830-6850-490a-8eaf-9d505e3e4671',
          },
          {
            name: 'Stripped',
            record_uLID: 'ec9b5fbe-6428-4a67-aab8-9a23cdce2f9f',
          },
        ],
      },
    ],
  },
  {
    vendor_uLID: '5e80971b1540161f3279e29e',
    productVariations: [
      {
        variationName: 'Pattern',
        variationOptions: [
          {
            name: 'Bold',
            record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0055',
          },
          {
            name: 'Spotted',
            record_uLID: '66654830-6850-490a-8eaf-9d505e3e4671',
          },
        ],
      },
    ],
  },
]

Output Array: Output 阵列:

    [
      {
        "variationName": "Colour",
        "variationOptions": [
          {
            "name": "Blue",
            "record_uLID": "6afa239e-ce53-40eb-addc-836d8ecc0051",
            "linkedVendors": [
              "5e793a0411d2bef2e375cd00",
              "5e7bb266071f9601b6ad8f4e",
            ]
          },
          {
            "name": "yellow",
            "record_uLID": "66654830-6850-490a-8eaf-9d505e3e4672",
            "linkedVendors": [
              "5e793a0411d2bef2e375cd00",
              "5e7bb266071f9601b6ad8f4e"
            ]
          }
        ]
      },
      {
        "variationName": "Pattern",
        "variationOptions": [
          {
            "name": "Bold",
            "record_uLID": "6afa239e-ce53-40eb-addc-836d8ecc0055",
            "linkedVendors": [
              "5e80971b1540161f3279e29e",
              "5e7bb266071f9601b6ad8f4e",
              "5e793a0411d2bef2e375cd00"
            ]
          },
          {
            "name": "Spotted",
            "record_uLID": "66654830-6850-490a-8eaf-9d505e3e4671",
            "linkedVendors": [
              "5e80971b1540161f3279e29e",
              "5e793a0411d2bef2e375cd00",
              "5e7bb266071f9601b6ad8f4e"

            ]
          },
          {
            "name": "Stripped",
            "record_uLID": "ec9b5fbe-6428-4a67-aab8-9a23cdce2f9f",
            "linkedVendors": [
             "5e793a0411d2bef2e375cd00",
             "5e7bb266071f9601b6ad8f4e"
            ]
          }
        ]
      }
    ]

v v

This transformation is a bit complex.这个转换有点复杂。 You'll have to do it in two passes.您必须分两次完成。 First pass to create a Map of record_uLID to a Set of vendor_uLID s.首先通过创建一个Maprecord_uLID到一vendor_uLID s。 Second pass to link the vendors to the variation options.第二遍将供应商链接到变体选项。 I've done this for you with my library.我已经用我的图书馆为您完成了这项工作。

 var arrayIn = [ { vendor_uLID: '5e793a0411d2bef2e375cd00', productVariations: [ { variationName: 'Colour', variationOptions: [ { name: 'Blue', record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0051', }, { name: 'yellow', record_uLID: '66654830-6850-490a-8eaf-9d505e3e4672', } ], }, { variationName: 'Pattern', variationOptions: [ { name: 'Bold', record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0055', }, { name: 'Spotted', record_uLID: '66654830-6850-490a-8eaf-9d505e3e4671', }, { name: 'Stripped', record_uLID: 'ec9b5fbe-6428-4a67-aab8-9a23cdce2f9f', }, ], }, ], }, { vendor_uLID: '5e7bb266071f9601b6ad8f4e', productVariations: [ { variationName: 'Colour', variationOptions: [ { name: 'Blue', record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0051', }, { name: 'yellow', record_uLID: '66654830-6850-490a-8eaf-9d505e3e4672', } ], }, { variationName: 'Pattern', variationOptions: [ { name: 'Bold', record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0055', }, { name: 'Spotted', record_uLID: '66654830-6850-490a-8eaf-9d505e3e4671', }, { name: 'Stripped', record_uLID: 'ec9b5fbe-6428-4a67-aab8-9a23cdce2f9f', }, ], }, ], }, { vendor_uLID: '5e80971b1540161f3279e29e', productVariations: [ { variationName: 'Pattern', variationOptions: [ { name: 'Bold', record_uLID: '6afa239e-ce53-40eb-addc-836d8ecc0055', }, { name: 'Spotted', record_uLID: '66654830-6850-490a-8eaf-9d505e3e4671', }, ], }, ], }, ] const { pipe, fork, assign, tap, map, reduce, get } = rubico const trace = tap(console.log) const identity = x => x const incMap = (m, { record_uLID, vendor_uLID }) => { if (m.has(record_uLID)) { m.get(record_uLID).add(vendor_uLID) } else { m.set(record_uLID, new Set([vendor_uLID])) } return m } const combineMaps = (mA, mB) => { for (const [record_uLID, linkedVendors] of mB) { for (const vendor_uLID of linkedVendors) { incMap(mA, { record_uLID, vendor_uLID }) } } return mA } /* * vendors => Map { record_uLID -> Set { vendor_uLID } } */ const createRecordToVendorMap = pipe([ map(vendor => pipe([ // for each vendor of vendors get('productVariations'), map(pipe([ // for each productVariation of productVariations get('variationOptions'), reduce(map(fork({ // for each record of each variationOption of each variationOptions, create a new object record_uLID: get('record_uLID'), vendor_uLID: () => vendor.vendor_uLID, }))(incMap), new Map()), // reduce the new object via incMap into a new Map() ])), reduce(combineMaps, new Map()), // combine array of Maps into one Map ])(vendor)), reduce(combineMaps, new Map()), // combine array of Maps into one Map ]) /* * vendors => variationOptions_with_linkedVendors */ const linkVendorsToProductVariations = pipe([ fork({ recordToVendorMap: createRecordToVendorMap, // vendors => Map { record_uLID => Set { vendor_uLID } } vendors: identity, }), ({ recordToVendorMap, vendors }) => map(pipe([ // for each vendor of vendors get('productVariations'), map(fork({ // for each productVariation of productVariations, create a new object { variationName, variationOptions } variationName: get('variationName'), variationOptions: pipe([ get('variationOptions'), map(assign({ // for each variationOption of variationOptions, assign the object {...variationOption, linkedVendors } linkedVendors: pipe([ get('record_uLID'), record_uLID => recordToVendorMap.get(record_uLID) || new Set(), Array.from, ]), })), ]), })), ]))(vendors), ]) console.log(JSON.stringify( linkVendorsToProductVariations(arrayIn), null, 2, ))
 <script src="https://unpkg.com/rubico/index.js" crossorigin></script>

I've commented the code some, but I recommend you take the tour to get a better grasp on what's happening.我已经对代码进行了一些评论,但我建议您参观一下以更好地了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM