heng sopheak

heng sopheak

  • NA
  • 3
  • 1.9k

How to combine all array within array key prefix Typescript

Oct 25 2018 5:41 AM
I'm creating the import from CSV files to my database using Typescript which is a new language for me but I got stuck with I tried to combine those array keys and its values to single objects for inserting to database.
 
Data after respond form CSV
https://jsoneditoronline.org/?id=201ab98636544503ab264176dc6ab06b
Code
  1. async importByURL(url: string, entityName: string, attributes: string[])  
  2. {  
  3. const csvData = await this.convertCSVToJSON(url, attributes);  
  4. const meta = getMetadataArgsStorage().relations;  
  5. let target = null;  
  6. const result = [];  
  7. for (const csvAttribute of csvData)  
  8. {  
  9. const allKeys = Object.keys(csvAttribute);  
  10. const allPrefixes = allKeys.map(key => key.split("_")[0]);  
  11. const uniquePrefixes = _.uniq(allPrefixes);  
  12. const entity = {};  
  13. const entityMeta = meta.filter(m =>  
  14. {  
  15. if (m.target["name"] == entityName)  
  16. {  
  17. target = m.target; return true;  
  18. }  
  19. return false;  
  20. });  
  21. uniquePrefixes.map(prefix =>  
  22. {  
  23. const matchedKeys = allKeys.filter(function (key)  
  24. {  
  25. const keyPrefix = key.split("_")[0];  
  26. if (keyPrefix == prefix)  
  27. {  
  28. return keyPrefix;  
  29. }  
  30. return false;  
  31. });  
  32. if (prefix == entityName)  
  33. {  
  34. matchedKeys.map(matchedKey =>  
  35. {  
  36. entity[matchedKey.split("_")[1]] = csvAttribute[matchedKey];  
  37. });  
  38. }  
  39. else  
  40. {  
  41. const matchedRelation = entityMeta.find(m => { const constructor = m.type as FunctionConstructor;   
  42. const func = constructor();  
  43. return func["name"] == prefix;  
  44. });  
  45. if (matchedRelation)  
  46. {  
  47. const classConstructor = matchedRelation.type as FunctionConstructor;  
  48. const classInstance = classConstructor() as FunctionConstructor;  
  49. const object = new classInstance();  
  50. matchedKeys.map(matchedKey => { object[matchedKey.split("_")[1]] = csvAttribute[matchedKey];  
  51. });  
  52. if (matchedRelation.relationType == "one-to-one")  
  53. {  
  54. entity[matchedRelation.propertyName] = object;  
  55. }  
  56. else if (matchedRelation.relationType == "many-to-one")  
  57. {  
  58. entity[matchedRelation.propertyName] = object;  
  59. }  
  60. else  
  61. {  
  62. entity[matchedRelation.propertyName] = [object]; } } } });  
  63. result.push(entity);  
  64. }  
  65. return this.resolveManyRelationships(result);  
  66. }  
  67. private resolveManyRelationships(result)  
  68. {  
  69. const finalResult = [];  
  70. const groupedBy = _.groupBy(result, (result) => result.code);  
  71. const keys = Object.keys(groupedBy);  
  72. for (const key of keys)  
  73. {  
  74. const values = groupedBy[key] as any[];  
  75. const originValue = {};  
  76. values.map(value => { const valueKeys = Object.keys(value);  
  77. valueKeys.map(valueKey =>  
  78. {  
  79. const propertyValue = value[valueKey];  
  80. if (propertyValue != null && _.isArray(propertyValue))  
  81. {  
  82. let originalValues = [];  
  83. originalValues.push(value[valueKey]);  
  84. if (originalValues == null)  
  85. {  
  86. originalValues = [];  
  87. }  
  88. const contentValue = value[valueKey];  
  89. originValue[valueKey] = [ ...originalValues, ...contentValue ];  
  90. }  
  91. else  
  92. {  
  93. if (!_.isEmpty(propertyValue))  
  94. {  
  95. originValue[valueKey] = propertyValue; } }  
  96. });  
  97. });  
  98. finalResult.push(originValue);  
  99. }  
  100. return finalResult;  
  101. // [{"finalResult": finalResult}, {"result": result}];  
  102. }  
Incorrect Result
https://jsoneditoronline.org/?id=e3da089dd18442f3ae5dd3781398eb3e