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
- async importByURL(url: string, entityName: string, attributes: string[])
- {
- const csvData = await this.convertCSVToJSON(url, attributes);
- const meta = getMetadataArgsStorage().relations;
- let target = null;
- const result = [];
- for (const csvAttribute of csvData)
- {
- const allKeys = Object.keys(csvAttribute);
- const allPrefixes = allKeys.map(key => key.split("_")[0]);
- const uniquePrefixes = _.uniq(allPrefixes);
- const entity = {};
- const entityMeta = meta.filter(m =>
- {
- if (m.target["name"] == entityName)
- {
- target = m.target; return true;
- }
- return false;
- });
- uniquePrefixes.map(prefix =>
- {
- const matchedKeys = allKeys.filter(function (key)
- {
- const keyPrefix = key.split("_")[0];
- if (keyPrefix == prefix)
- {
- return keyPrefix;
- }
- return false;
- });
- if (prefix == entityName)
- {
- matchedKeys.map(matchedKey =>
- {
- entity[matchedKey.split("_")[1]] = csvAttribute[matchedKey];
- });
- }
- else
- {
- const matchedRelation = entityMeta.find(m => { const constructor = m.type as FunctionConstructor;
- const func = constructor();
- return func["name"] == prefix;
- });
- if (matchedRelation)
- {
- const classConstructor = matchedRelation.type as FunctionConstructor;
- const classInstance = classConstructor() as FunctionConstructor;
- const object = new classInstance();
- matchedKeys.map(matchedKey => { object[matchedKey.split("_")[1]] = csvAttribute[matchedKey];
- });
- if (matchedRelation.relationType == "one-to-one")
- {
- entity[matchedRelation.propertyName] = object;
- }
- else if (matchedRelation.relationType == "many-to-one")
- {
- entity[matchedRelation.propertyName] = object;
- }
- else
- {
- entity[matchedRelation.propertyName] = [object]; } } } });
- result.push(entity);
- }
- return this.resolveManyRelationships(result);
- }
- private resolveManyRelationships(result)
- {
- const finalResult = [];
- const groupedBy = _.groupBy(result, (result) => result.code);
- const keys = Object.keys(groupedBy);
- for (const key of keys)
- {
- const values = groupedBy[key] as any[];
- const originValue = {};
- values.map(value => { const valueKeys = Object.keys(value);
- valueKeys.map(valueKey =>
- {
- const propertyValue = value[valueKey];
- if (propertyValue != null && _.isArray(propertyValue))
- {
- let originalValues = [];
- originalValues.push(value[valueKey]);
- if (originalValues == null)
- {
- originalValues = [];
- }
- const contentValue = value[valueKey];
- originValue[valueKey] = [ ...originalValues, ...contentValue ];
- }
- else
- {
- if (!_.isEmpty(propertyValue))
- {
- originValue[valueKey] = propertyValue; } }
- });
- });
- finalResult.push(originValue);
- }
- return finalResult;
- // [{"finalResult": finalResult}, {"result": result}];
- }
Incorrect Result
https://jsoneditoronline.org/?id=e3da089dd18442f3ae5dd3781398eb3e

Replies
Know the answer? Post it — somebody with the same question will find it here.