Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
How to trim both key and value from an array of objects using lodash

I have an array of objects like below.

 {
    "Id": 11,
    "name ": "xxN "
  }

How can I use lodash trim method to trim both key and value.

 {
    "Id": 11,
    "name": "xxN"
  }

Expected:

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Does it have to use Lodash? With vanilla JavaScript:

result = Object.fromEntries(Object.entries(data).map(([key, value]) => 
    [key.trim(), typeof value == 'string' ? value.trim() : value]))
about 4 years ago · Juan Pablo Isaza Report

0

For completeness, here's a Lodash solution using _.transform()

const obj = {
  "Id": 11,
  "name ": "xxN "
}

const transformed = _.transform(obj, (result, value, key) => {
  result[key.trim()] = typeof value === "string" ? value.trim() : value
})

console.log(transformed)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

This has the added bonus of producing a new object with the same prototype as your original which may or may not be handy.

about 4 years ago · Juan Pablo Isaza Report

0

Using lodash trim method

const arr = [
  {
    Id: 11,
    "name ": "xxN ",
  },
  {
    Id: 12,
    "name ": " yyK ",
  },
];

const result = arr.map((o) =>
  Object.fromEntries(
    Object.entries(o).map(([k, v]) => [k, typeof v === "string" ? _.trim(v) : v])
  )
);

console.log(result);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!