kwhitley / treeize

Converts row data (in JSON/associative array format) to tree structure based on column naming conventions.
MIT License
189 stars 24 forks source link

Flat array don't merge correctly #47

Open dharijanto opened 5 years ago

dharijanto commented 5 years ago

Hello, I have this flattened array I'm expecting to merge correctly, but isn't:

const flatArray =[ { id: 4,                                                                                                                       
    shopId: 1,                                                                                                                                                                                                     
    name: 'SanDisk Cruzer 32GB',                                                                                                                                                                                   
    description: 'inStockProductsView.description',                                                                                                                                          
    warranty: null,                                                                                                          
    price: 165000,                                                                                                                                                                                                 
    stockQuantity: '5',                                                                                                                                                                                            
    'primaryImage.imageFilename': '1532682092960_1.png',                                                                                                                                                  
    'primaryImage.productId': 4,                                                                                                                                                                             
    'images.imageFilename': '1532682092960_1.png',
    'images.productId': 4,
    'subCategory.id': 1,   
    'subCategory.name': 'Flash Drive',
    'subCategory.description': '',
    'subCategory.categoryId': 1,                               
    'subCategory.imageFilename': '1532656377590_category-flash-drive.png',
    'subCategory.category.id': 1,                                      
    'subCategory.category.name': 'Aksesoris Komputer',                     
    'subCategory.category.description': '',
    'variants.id': 9,
    'variants.shopId': 1,       
    'variants.productId': 4,                       
    'variants.name': 'Hitam',
    'variants.stockQuantity': '5' },
  { id: 4,   
    shopId: 1, 
    name: 'SanDisk Cruzer 32GB',
    description: 'inStockProductsView.description',
    warranty: null,    
    price: 165000,              
    stockQuantity: '5',
    'primaryImage.imageFilename': '1532682092960_1.png',
    'primaryImage.productId': 4,
    'images.imageFilename': '1532682113935_2.png',
    'images.productId': 4,
    'subCategory.id': 1,                                       
    'subCategory.name': 'Flash Drive',                                    
    'subCategory.description': '',                                     
    'subCategory.categoryId': 1,                                            
    'subCategory.imageFilename': '1532656377590_category-flash-drive.png',                                                            
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 9,
    'variants.shopId': 1,
    'variants.productId': 4,
    'variants.name': 'Hitam',
    'variants.stockQuantity': '5' } ]

const tree = new Treeize()
  tree.setOptions({
    input: {
      delimiter: '.'
    }
  })
  const result = tree.grow(flatArray).getData()

The flat array doesn't merge correctly, the result is like this:

[ { id: 4,                                                                                                                                                                                                [85/1993]
    shopId: 1,                                                                                                                                                                                                     
    name: 'SanDisk Cruzer 32GB',                                                                                                         
    description: 'inStockProductsView.description',                                                                              
    price: 165000,                                                                                                                                                                                                 
    stockQuantity: '5',                                                                                                                                                                                            
    variants:                                                                                                                                                                                
     [ { id: 9,                                                                                                              
         shopId: 1,                                                                                                                                                                                                
         productId: 4,                                                                                                                                                                                             
         name: 'Hitam',                                                                                                                                                                                   
         stockQuantity: '5' } ],                                                                                                                                                                             
    subCategory:                                  
     { id: 1,             
       name: 'Flash Drive',
       description: '',               
       categoryId: 1,             
       imageFilename: '1532656377590_category-flash-drive.png',
       category: { id: 1, name: 'Aksesoris Komputer', description: '' } },
    images: [ { imageFilename: '1532682092960_1.png', productId: 4 } ],
    primaryImage: { imageFilename: '1532682092960_1.png', productId: 4 } },
  { id: 4,                                 
    shopId: 1,       
    name: 'SanDisk Cruzer 32GB',
    description: 'inStockProductsView.description',
    price: 165000,           
    stockQuantity: '5',             
    variants:
     [ { id: 9,
         shopId: 1,             
         productId: 4,                             
         name: 'Hitam',
         stockQuantity: '5' } ],
    subCategory:       
     { id: 1,                                           
       name: 'Flash Drive',     
       description: '',                           
       categoryId: 1,     
       imageFilename: '1532656377590_category-flash-drive.png',
       category: { id: 1, name: 'Aksesoris Komputer', description: '' } },
    images: [ { imageFilename: '1532682113935_2.png', productId: 4 } ],
    primaryImage: { imageFilename: '1532682092960_1.png', productId: 4 } } ]

On the other hand, I have the following flat array:

[ { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    'primaryImage.imageFilename': '1532660195494_3.png',
    'primaryImage.productId': 5,
    'images.imageFilename': '1532660186147_1.png',
    'images.productId': 5,
    'subCategory.id': 3,
    'subCategory.name': 'Mouse',
    'subCategory.description': '',
    'subCategory.categoryId': 1,
    'subCategory.imageFilename': '1532656280595_category-mouse.png',
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 11,
    'variants.shopId': 1,
    'variants.productId': 5,
    'variants.name': 'Grey',
    'variants.stockQuantity': '5' },
  { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    'primaryImage.imageFilename': '1532660195494_3.png',
    'primaryImage.productId': 5,
    'images.imageFilename': '1532660186147_1.png',
    'images.productId': 5,
    'subCategory.id': 3,
    'subCategory.name': 'Mouse',
    'subCategory.description': '',
    'subCategory.categoryId': 1,
    'subCategory.imageFilename': '1532656280595_category-mouse.png',
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 10,
    'variants.shopId': 1,
    'variants.productId': 5,
    'variants.name': 'Hitam',
    'variants.stockQuantity': '12' },
  { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    'primaryImage.imageFilename': '1532660195494_3.png',
    'primaryImage.productId': 5,
    'images.imageFilename': '1532660190895_2.png',
    'images.productId': 5,
    'subCategory.id': 3,
    'subCategory.name': 'Mouse',
    'subCategory.description': '',
    'subCategory.categoryId': 1,
    'subCategory.imageFilename': '1532656280595_category-mouse.png',
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 11,
    'variants.shopId': 1,
    'variants.productId': 5,
    'variants.name': 'Grey',
    'variants.stockQuantity': '5' },
  { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    'primaryImage.imageFilename': '1532660195494_3.png',
    'primaryImage.productId': 5,
    'images.imageFilename': '1532660190895_2.png',
    'images.productId': 5,
    'subCategory.id': 3,
    'subCategory.name': 'Mouse',
    'subCategory.description': '',
    'subCategory.categoryId': 1,
    'subCategory.imageFilename': '1532656280595_category-mouse.png',
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 10,
    'variants.shopId': 1,
    'variants.productId': 5,
    'variants.name': 'Hitam',
    'variants.stockQuantity': '12' },
  { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    'primaryImage.imageFilename': '1532660195494_3.png',
    'primaryImage.productId': 5,
    'images.imageFilename': '1532660195494_3.png',
    'images.productId': 5,
    'subCategory.id': 3,
    'subCategory.name': 'Mouse',
    'subCategory.description': '',
    'subCategory.categoryId': 1,
    'subCategory.imageFilename': '1532656280595_category-mouse.png',
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 11,
    'variants.shopId': 1,
    'variants.productId': 5,
    'variants.name': 'Grey',
    'variants.stockQuantity': '5' },
  { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    'primaryImage.imageFilename': '1532660195494_3.png',
    'primaryImage.productId': 5,
    'images.imageFilename': '1532660195494_3.png',
    'images.productId': 5,
    'subCategory.id': 3,
    'subCategory.name': 'Mouse',
    'subCategory.description': '',
    'subCategory.categoryId': 1,
    'subCategory.imageFilename': '1532656280595_category-mouse.png',
    'subCategory.category.id': 1,
    'subCategory.category.name': 'Aksesoris Komputer',
    'subCategory.category.description': '',
    'variants.id': 10,
    'variants.shopId': 1,
    'variants.productId': 5,
    'variants.name': 'Hitam',
    'variants.stockQuantity': '12' } ]

Which successfully merges into:

[ { id: 5,
    shopId: 1,
    name: 'Logitech M235',
    description: 'inStockProductsView.description',
    warranty: '1 tahun',
    price: 180000,
    stockQuantity: '17',
    variants:
     [ { id: 11,
         shopId: 1,
         productId: 5,
         name: 'Grey',
         stockQuantity: '5' },
       { id: 10,
         shopId: 1,
         productId: 5,
         name: 'Hitam',
         stockQuantity: '12' } ],
    subCategory:
     { id: 3,
       name: 'Mouse',
       description: '',
       categoryId: 1,
       imageFilename: '1532656280595_category-mouse.png',
       category: { id: 1, name: 'Aksesoris Komputer', description: '' } },
    images:
     [ { imageFilename: '1532660186147_1.png', productId: 5 },
       { imageFilename: '1532660190895_2.png', productId: 5 },
       { imageFilename: '1532660195494_3.png', productId: 5 } ],
    primaryImage: { imageFilename: '1532660195494_3.png', productId: 5 } } ]

I wonder where's the problem. Any pointer would be highly appreciated!

Thanks in advance

dharijanto commented 5 years ago

I found the problem. Apparently it's because 'warranty' field on the flat array is null.

Referencing issue #35