1

I have got a list of array objects as shown below

[
 {
   "subjectID": 1
   "Chosen" : "{subjectsChosen:Python,java,Angular}"
   "password": "{studentpw:123456abcd}"
 },
 {
   "subjectID": 2
   "Chosen" : "{subjectsChosen:SQL,Rprogram,React}"
   "password": "{studentpw:987654zyxwv}"
 }
]

Here I would like to remove the special characters and its notations and expected to populate array as shown below using typescript

[
 {
   "subjectID": 1
   "Chosen" : "Python,java,Angular"
   "password": "23456abcd"
 },
 {
   "subjectID": 2
   "Chosen" : "SQL,Rprogram,React"
   "password": "987654zyxwv"
 }
]

Thank you in advance

1
  • Welcome, what have you tried? I'd recommend checking out the map function. Commented Jan 19, 2022 at 18:27

3 Answers 3

1

try this

for (let i = 0; i < subs.length; i++) {
  subs[i].Chosen = removeSymbols(subs[i].Chosen);
  subs[i].password = removeSymbols(subs[i].password);
}

function removeSymbols(s: string) {
  return s.replace(/[\{\}]/g, "").split(":")[1];
}

result

[
  {
    "subjectID": 1,
    "Chosen": "Python,java,Angular",
    "password": "123456abcd"
  },
  {
    "subjectID": 2,
    "Chosen": "SQL,Rprogram,React",
    "password": "987654zyxwv"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

0

Welcome, is your object properties special characters limited to '{', ':', '}', if so, I propose to you the bellow solution, that I have tried and give a result as the one you have expected:

let objs = [
    {
        subjectID: 1,
        Chosen: "{subjectsChosen:Python,java,Angular}",
        password: "{studentpw:123456abcd}"
    },
    {
        subjectID: 2,
        Chosen: "{subjectsChosen:SQL,Rprogram,React}",
        password: "{studentpw:987654zyxwv}",
    }
];

objs.forEach((cur) => {
    Object.keys(cur).forEach(key => {
        if (typeof cur[key] === 'string') {
            cur[key]=cur[key].replace(/[\{\}]/g, '').split(':')[1];
        }
    })
});

console.log(objs);

You could use any array operator function other than forEach.

Comments

0

We could use map operator of Array here to transform each item. To transform Chosen and password fields, we could use Regex and replace method of string.

const chosenRegex = new RegExp(/\{subjectsChosen:(.+)\}/, 'i')
const myText = "{subjectsChosen:Python,java,Angular}";

myText.replace(re, '$1'); // Python,java,Angular

Below is the full implementation that transform each item.

const items = [
 {
   "subjectID": 1,
   "Chosen" : "{subjectsChosen:Python,java,Angular}",
   "password": "{studentpw:123456abcd}"
 },
 {
   "subjectID": 2,
   "Chosen" : "{subjectsChosen:SQL,Rprogram,React}",
   "password": "{studentpw:987654zyxwv}"
 }
];

const chosenRegex = new RegExp(/\{subjectsChosen:(.+)\}/, 'i')
const passwordRegex = new RegExp(/\{studentpw:(.+)\}/, 'i')

const transformedItems = items.map(item => {
    return {
      ...item,
      "Chosen": item.Chosen.replace(chosenRegex, '$1'),
      "password": item.password.replace(passwordRegex, '$1')
    }
});

console.log(transformedItems);

We could also literally use a single regex if we don't want to differentiate them.

const transformRegex = new RegExp(/\{(.+):(.+)\}/, 'i');

....

return {
 ...item,
 "Chosen": item.Chosen.replace(transformRegex, '$2'), // Since there are two regex groups now, use $2
 "password": item.password.replace(transformRegex, '$2')
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.