In my collection of objects, there is a specific string mentioned:
[
{
"id": 2240,
"relatedId": "1000"
},
{
"id": 1517,
"relatedId": "100200"
},
{
"id": 1517,
"relatedId": "{{100200}}"
},
{
"id": 2236,
"relatedId": "{{271}}+{{269}}+{{267}}"
}
]
My objective is to extract the related ID keys that contain "{{" or "}}" in them. Then, I intend to remove any non-numeric characters and provide an array of these numbers. The resulting array should be:
[100200,271,269,267]
This code achieves the desired result:
const objArray = [
{
"id": 2240,
"relatedId": "1000"
},
{
"id": 1517,
"relatedId": "100200"
},
{
"id": 1517,
"relatedId": "{{100200}}"
},
{
"id": 2236,
"relatedId": "{{271}}+{{269}}+{{267}}"
}
];
const numArray = objArray.map(v => v.relatedId).filter(el => el.includes('{{'))
.map(v => v.replace(/[{{}}]/g,'').split('+')).flat()
.map(Number);
console.log(numArray)
Is there a more elegant way to accomplish this task? It seems like there should be a simpler solution.