I have data like this:
Record= [
{ "tid": 1, "token_text": "Canis", "spanid": 1, "label": "Name" },
{ "tid": 2, "token_text": "Familiaris", "spanid": 1, "label": "Name" },
{ "tid": 3, "token_text": "is" },
{ "tid": 4, "token_text": "the" },
{ "tid": 5, "token_text": "scientific" },
{ "tid": 6, "token_text": "name" },
{ "tid": 7, "token_text": "of" },
{ "tid": 8, "token_text": "dog", "spanid": 2, "label": "species" },
{ "tid": 9, "token_text": "." }
]
I want to create another array of dictionaries like the data where the token_text strings that have the same spanid are joined and the tid and label_name can be the same as that of the first occurrence. I want to do this while keeping the order of the elements in the array the same as that of the Record array. I want to do this in JS/ React. I will appreciate any leads in this matter. The output will look like the following:
Record = [
{ "tid": 1, "token_text": "Canis Familiaris", "spanid": 1, "label": "Name" },
{ "tid": 3, "token_text": "is" },
{ "tid": 4, "token_text": "the" },
{ "tid": 5, "token_text": "scientific" },
{ "tid": 6, "token_text": "name" },
{ "tid": 7, "token_text": "of" },
{ "tid": 8, "token_text": "dog", "spanid": 2, "label": "species" },
{ "tid": 9, "token_text": "." }
]
I tried looping but couldn’t figure out exactly.
for (let j = 0; j < spanid.length; j++) {
var arr = [];
Records.forEach(i => {
if (i.spanid === spanid[j]) {
arr.push(i.tid);
}
})
}
Is there a way to do it like this to join the token_texts with same spanids and delete the instances where that have duplicate spanids?
3
Answers
One solution is to use a
for
loop and anif
statement to iterate over the array and check thespanid
value of each object.For example:
You can use a nested loop to handle the cases where there are more than two
token_text
strings with the samespanid
. This way, you can combine all those strings into one with a space between them.For example:
You can reduce the array of tokens, and check the previous token’s label against the current.
In order to append to the previous
token_text
you need to verify three things:label
Working example
Note: You could shorten the if-condition (below) to simply: