TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
heng sopheak
NA
3
2k
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
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
Reply
Answers (
0
)
Cant write advanced projects
Verification and reset password emails aren't sent