I have this JSON object:
<code>{
"id": 504,
"name": "TITLE",
"assetType": "TEXT",
"translatedText": [
{
"country": "IT",
"text": "Samsung Crystal UHD 4K GU65DU8079UXZG Televisore, 65″, nero"
},
{
"country": "US",
"text": "Samsung Crystal UHD 4K GU65DU8079UXZG TV, 65″, black"
},
{}
],
"translatedHtml": [],
"translatedImage": [],
"translatedThumbnail": []
}
</code>
<code>{
"id": 504,
"name": "TITLE",
"assetType": "TEXT",
"translatedText": [
{
"country": "IT",
"text": "Samsung Crystal UHD 4K GU65DU8079UXZG Televisore, 65″, nero"
},
{
"country": "US",
"text": "Samsung Crystal UHD 4K GU65DU8079UXZG TV, 65″, black"
},
{}
],
"translatedHtml": [],
"translatedImage": [],
"translatedThumbnail": []
}
</code>
{
"id": 504,
"name": "TITLE",
"assetType": "TEXT",
"translatedText": [
{
"country": "IT",
"text": "Samsung Crystal UHD 4K GU65DU8079UXZG Televisore, 65″, nero"
},
{
"country": "US",
"text": "Samsung Crystal UHD 4K GU65DU8079UXZG TV, 65″, black"
},
{}
],
"translatedHtml": [],
"translatedImage": [],
"translatedThumbnail": []
}
I need to create a map<string, JSON>
using country as key and "text": "Samsung Crystal UHD 4K GU65DU8079UXZG TV, 65″, black"
as value.
I tried many solutions but without success.
<code>for (const item of assertForm.translatedText) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const jsonObject: JSON = JSON.parse(JSON.stringify({ text: item.text }));
console.log(`Value of country:`, item.country);
console.log(`Value of jsonObject:`, jsonObject);
translatedAssetContainer.value.set(item.country, jsonObject);
}
</code>
<code>for (const item of assertForm.translatedText) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const jsonObject: JSON = JSON.parse(JSON.stringify({ text: item.text }));
console.log(`Value of country:`, item.country);
console.log(`Value of jsonObject:`, jsonObject);
translatedAssetContainer.value.set(item.country, jsonObject);
}
</code>
for (const item of assertForm.translatedText) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const jsonObject: JSON = JSON.parse(JSON.stringify({ text: item.text }));
console.log(`Value of country:`, item.country);
console.log(`Value of jsonObject:`, jsonObject);
translatedAssetContainer.value.set(item.country, jsonObject);
}
or
<code>const convertedObject: Map<string, JSON> = new Map(
assertForm.translatedText.map((item) => [item.country, JSON.parse(JSON.stringify({ text: item.text }))])
);
</code>
<code>const convertedObject: Map<string, JSON> = new Map(
assertForm.translatedText.map((item) => [item.country, JSON.parse(JSON.stringify({ text: item.text }))])
);
</code>
const convertedObject: Map<string, JSON> = new Map(
assertForm.translatedText.map((item) => [item.country, JSON.parse(JSON.stringify({ text: item.text }))])
);
In all cases, the map is empty. What am I doing wrong?