JSFiddle - React, Tailwind, and code Playground

by Jussia

JavaScript

var products = [
{
  	"ItemDescription":"just product 1",
		"QuantityDespatched":"11",
    "Type":"",
		"ID-Begin":"",
    "packingLevel":1,
		"packingId":""
  },
  {
  	"ItemDescription":"in pack22",
		"QuantityDespatched":"22",
    "Type":"",
		"ID-Begin":"",
    "packingLevel":1,
		"packingId":"122222222200000484",
  },
	{
  	"Type":"BJ",
		"ID-Begin":"122222222200000484",
    "packingLevel":0,
		"packingId":"",
  },
  {
  	"ItemDescription":"in pack11",
		"QuantityDespatched":"11",
    "Type":"",
		"ID-Begin":"",
    "packingLevel":2,
		"packingId":"122222222200000485",
  },
	{
  	"Type":"BJ",
		"ID-Begin":"122222222200000485",
    "packingLevel":1,
		"packingId":"122222222200000515",
  },
  {
  	"Type":"NUL",
		"ID-Begin":"122222222200000515",
    "packingLevel":0,
		"packingId":"122222222200000485",
    packing: {
      "Type":"BJ",
      "ID-Begin":"122222222200000485",
      "packingLevel":1,
      "packingId":"122222222200000515",
  }
  }
]

/**
	 * prepare products values to the needed structure
	 */

	var changeProductValueStructure = function(packing) {
		var newPacking = [];

		packing.forEach(function(pack) {
			var lastItem = newPacking[newPacking.length - 1];
      
        if (lastItem) {
      
      if (pack.hasOwnProperty('packing')) {
      	 if (lastItem.packing['packingId'] === pack['ID-Begin']) {
         	delete pack.packing
          lastItem.packing.packing = pack;
					pack = lastItem
         }
         }
      
      else if (lastItem["Type"] == "" && pack["Type"] !== ""){
					lastItem.packing = pack;
					pack = lastItem
				}
        
        var isProductWithoutPack = pack["Type"] === "" && pack["ID-Begin"] === "" && pack["packingId"] === "";
			if (isProductWithoutPack) {
				pack.packingLevel = 0
			}
		
      }
      
      if (lastItem) {
        if (lastItem.ItemDescription !== pack.ItemDescription) {
            newPacking.push(pack)
          }
      }
      else {
      newPacking.push(pack)
      }
    ...