I have an array of json objects like below. Each object has permanent key 'type' and depending on the 'type', new keys are added.
So if type: 'text', we have a new key 'text'.
If type: 'notText', we have a new key 'attrs'.
arrayOfObj = [
{
"type": "text",
"text": "="
},
{
"type": "text",
"text": " "
},
{
"type": "text",
"text": "S"
},
{
"type": "text",
"text": "O"
},
{
"type": "notText",
"attrs": {
"id": 20,
"data": "Something",
}
}
]
Depending on the 'type' of each item i.e. if type: 'text', then I need to combine each 'text' into 1 object like so:
arrayOfObj = [
{
"type": "text",
"text": "= SO"
},
{
"type": "notText",
"attrs": {
"id": 20,
"data": "Something",
}
}
]
I know that to start it I can use
if(this.arrayOfObj.map(ed=>ed.type) === 'text') {
Object.assign({}, ...arrayOfObj);
}
However it doesn't quite work and I'm unsure of how to go further.
Would anyone have any idea of how to accomplish this?
textandattr? What should we do then?"type": "notText"object into one?