Reputation: 129
I have array structure as shown in below
[
{
"id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
"refId": "1",
"reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
"list": [
{
"text": "<p>Annot 1</p>",
"id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
},
{
"text": "<p>Annot 1</p>",
"id": "e1486639-aa46-4513-992b-17d8d66f06c8"
}
]
},
{
"id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
"refId": "2",
"reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
"list": [
{
"text": "<p>Annot 2</p>",
"id": "fc430895-912a-4e4b-884c-f08600e06ec8"
},
{
"text": "<p>Annot 2 3</p>",
"id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
},
{
"text": "<p>Annot 2 3</p>",
"id": "090737d5-48ac-4e3f-973d-9e1de206252b"
},
{
"text": "<p>Annot 2</p>",
"id": "6d5f89be-305e-4751-a7ca-75d9b0f2057e"
}
]
},
{
"id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
"refId": "3",
"reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
"list": [
{
"text": "<p>Annot 3</p>",
"id": "f9082af8-3c2f-477b-b113-a96f320415d1"
},
{
"text": "<p>Annot 3</p>",
"id": "2d7e65b8-e516-40bf-8957-c0fb4b819aa9"
}
]
}
]
Expected result is
[
{
"id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
"refId": "1",
"reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
"list": [
{
"text": "<p>Annot 1</p>",
"id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
}
]
},
{
"id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
"refId": "2",
"reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
"list": [
{
"text": "<p>Annot 2</p>",
"id": "fc430895-912a-4e4b-884c-f08600e06ec8"
},
{
"text": "<p>Annot 2 3</p>",
"id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
}
]
},
{
"id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
"refId": "3",
"reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
"list": [
{
"text": "<p>Annot 3</p>",
"id": "f9082af8-3c2f-477b-b113-a96f320415d1"
}
]
}
]
I have tried this solution but didnot get the expected result.
const finalListmergedArray = [
{
"id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
"refId": "1",
"reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
"list": [
{
"text": "<p>Annot 1</p>",
"id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
},
{
"text": "<p>Annot 1</p>",
"id": "e1486639-aa46-4513-992b-17d8d66f06c8"
}
]
},
{
"id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
"refId": "2",
"reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
"list": [
{
"text": "<p>Annot 2</p>",
"id": "fc430895-912a-4e4b-884c-f08600e06ec8"
},
{
"text": "<p>Annot 2 3</p>",
"id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
},
{
"text": "<p>Annot 2 3</p>",
"id": "090737d5-48ac-4e3f-973d-9e1de206252b"
},
{
"text": "<p>Annot 2</p>",
"id": "6d5f89be-305e-4751-a7ca-75d9b0f2057e"
}
]
},
{
"id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
"refId": "3",
"reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
"list": [
{
"text": "<p>Annot 3</p>",
"id": "f9082af8-3c2f-477b-b113-a96f320415d1"
},
{
"text": "<p>Annot 3</p>",
"id": "2d7e65b8-e516-40bf-8957-c0fb4b819aa9"
}
]
}
]
// Loop through array values
for (let i = 0; i < finalListmergedArray.length; i++) {
for (
let j = 0;
j < finalListmergedArray[i]["list"].length;
j++
) {
var result = [
...new Map(
finalListmergedArray[i]["list"].map((o) => [
JSON.stringify(o),
o,
])
).values(),
];
finalListmergedArray[i]["list"] = result;
}
}
console.log(finalListmergedArray)
Any help would be appreciated. Thanks, in advance.
Upvotes: 1
Views: 94
Reputation: 888
Assuming you want to merge with text in the list, you can use a list to store the processed elements. Below is the code snipped for the same.
const input = [
{
"id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
"refId": "1",
"reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
"list": [
{
"text": "<p>Annot 1</p>",
"id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
},
{
"text": "<p>Annot 1</p>",
"id": "e1486639-aa46-4513-992b-17d8d66f06c8"
}
]
},
{
"id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
"refId": "2",
"reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
"list": [
{
"text": "<p>Annot 2</p>",
"id": "fc430895-912a-4e4b-884c-f08600e06ec8"
},
{
"text": "<p>Annot 2 3</p>",
"id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
},
{
"text": "<p>Annot 2 3</p>",
"id": "090737d5-48ac-4e3f-973d-9e1de206252b"
},
{
"text": "<p>Annot 2</p>",
"id": "6d5f89be-305e-4751-a7ca-75d9b0f2057e"
}
]
},
{
"id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
"refId": "3",
"reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
"list": [
{
"text": "<p>Annot 3</p>",
"id": "f9082af8-3c2f-477b-b113-a96f320415d1"
},
{
"text": "<p>Annot 3</p>",
"id": "2d7e65b8-e516-40bf-8957-c0fb4b819aa9"
}
]
}
]
//Solution....
input.forEach(function(i) {
let processed = [];
const newList = [];
i.list.forEach(function(l) {
if( processed.indexOf(l.text) < 0 ) { //id not in list
newList.push(l);
processed.push(l.text);
}
});
i.list = newList;
});
console.log(input);
Upvotes: 1
Reputation: 10382
you want to remove text
duplicates values from list
attribute. To be clear, those objects are actually unique because each one has an unique id.
An approach to solve this is to create a Map
to store unique text values for each object. Then you filter your list
, validate if text
is included at your Map
. If it's included you return false, otherwise you add text
to your Map
and return true:
// data here is your array
const filteredData = data.map(item => {
const includedText = new Map()
const list = item.list.filter(content => {
if (includedText.has(content.text)) return false
includedText.set(content.text)
return true
})
return { ...item, list }
})
console.log(filteredData)
Upvotes: 1