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

182
Views
JavaScript modified some value in original JSON

I have:

const parsedData = [
  { id: 1, key: 'asdas'},
  { id: 2, key: 'asdas'},
]
parsedData.map((item, i) => ({
  ...item,
  key: `modified ${item.key}`
}));
console.log(parsedData);

What I'm trying is to get parsedData array modified and get:

[
  { id: 1, key: 'modified asdas'},
  { id: 2, key: 'modified asdas'},
]

But I'm getting the same array in the console.log

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

0

a map will return a new array you need to capture it in different variable

const parsedData = [
  { id: 1, key: 'asdas'},
  { id: 2, key: 'asdas'},
]

const newData = parsedData.map((item, i) => ({
  ...item,
  key: `modified ${item.key}`
}));

console.log(newData);
about 4 years ago · Juan Pablo Isaza Report

0

map returns the modified array; it does not mutate the original.

You should be logging the result returned by map:

const parsedData = [
  { id: 1, key: 'asdas'},
  { id: 2, key: 'asdas'},
]
let res = parsedData.map((item, i) => ({
  ...item,
  key: `modified ${item.key}`
}));
console.log(res);

Alternatively, you can use forEach:

const parsedData = [
  { id: 1, key: 'asdas'},
  { id: 2, key: 'asdas'},
]
parsedData.forEach((item, i) => (parsedData[i] = {
  ...item,
  key: `modified ${item.key}`
}));
console.log(parsedData);

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!