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

229
Views
Javascript function to modify the array of objects reuturns the same array

I have a data inside of my array, which looks like this.

const arr = [
  {
  devSth: {
    h1: [1],
    h2: [1],
  }
 },
];

I have a function, which should modify this array. It looks like this:

const modifyArr = (data) => {
 data.forEach(el => {
  let keys = el.devSth && Object.keys(el.devSth);
  keys.forEach(key => {
    el.devSth[key].map(el => {
    return {
    name: el,
    val: 'lalala'
    }
    })
  })
})
return data;
}

Then I am using my function:

const newArr = modifyArr(arr);
console.log(newArr);

Expected output:

[{
  devSth: {
    h1: [{name: 1, val: 'lalala'}],
    h2: [{name: 1, val: 'lalala'}]
  }
}]

Actual output:

[{
  devSth: {
    h1: [1],
    h2: [1]
  }
}]

Would appreciate any help.

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

0

you are mapping the values, but not setting them (line 5) :

const modifyArr = (data) => {
 data.forEach(el => {
  let keys = el.devSth && Object.keys(el.devSth);
  keys.forEach(key => {
    el.devSth[key] = el.devSth[key].map(el => {
    return {
    name: el,
    val: 'lalala'
    }
    })
  })
})
return data;
}

this is untested.

about 4 years ago · Juan Pablo Isaza Report

0

You dont assign the result of the .map operation back to the original keys h1 and h2. So it doesn't modify those.

Just change

            el.devSth[key].map(el => {
                return {
                    name: el,
                    val: 'lalala'
                }
            })

to

            el.devSth[key] = el.devSth[key].map(el => {
                return {
                    name: el,
                    val: 'lalala'
                }

const arr = [{
  devSth: {
    h1: [1],
    h2: [1],
  }
}, ];

const modifyArr = (data) => {
  data.forEach(el => {
    let keys = el.devSth && Object.keys(el.devSth);
    keys.forEach(key => {
      el.devSth[key] = el.devSth[key].map(el => {
        return {
          name: el,
          val: 'lalala'
        }
      })
    })
  })
  return data;
}

const newArr = modifyArr(arr);
console.log(newArr);

about 4 years ago · Juan Pablo Isaza Report

0

An alternative way of doing it:

const arr = [{
  devSth: {h1: [1], h2: [1]}
}];

let obj = arr[0].devSth, number;

for (const prop in obj){
  number = obj[prop][0];
  obj[prop] = [{name: number, val: 'lalala'}];
}

console.log([{devSth: obj}])

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!