2

I have object having string as keys and values. I want to trim all values of object in one go without need to check every index manually.

So, I have to iterate over object and will trim the value and the update value in object.

Can you share a way to iterate any object in Angulario 5.

let obj = {
  "name" => "    keshav     ",
  "prof" => "    Engineer   "
}

And I would like to this to convert into

let obj = {
  "name" => "keshav",
  "prof" => "Engineer"
}
2

3 Answers 3

4

You can use Object.entries:

Object.entries(obj).forEach(([key, value]) => obj[key] = value.trim());

This way you maintain the reference of the object

Sign up to request clarification or add additional context in comments.

Comments

1

you have to do like this, get all the object properties and then just trim it

 const properties = Object.keys(obj );
 properties.forEach(prop=>{
   obj[prop]= obj[prop].trim();  
 });

Comments

0

you can us simple for as well :

for(index in obj) { 
   obj[index] = obj[index].trim();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.