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

94
Views
Update array object in foreach loop with javascript

I am trying to create a new array. I have list of plugins with different versions, and I need to find plugins with same uniqueIdentifier but different versions, and create one array out of it. Example:

  {
    "uniqueIdentifier": "theme_test",
    "version": "2011120800"
  },
  {
    "uniqueIdentifier": "theme_test",
    "version": "3011120800"
  },
  {
    "uniqueIdentifier": "theme_test",
    "version": "4011120800"
  },

to be like this:

{
    "uniqueIdentifier": "theme_test",
    "version": [
      "2011120800",
      "3011120800",
      "4011120800"
    ]
}

So, in my code I am getting all the information, but I cannot make it work to store this versions as an array. So I am checking the uniqueIdentifier and then the version, and trying to generate new array:

item.pluginVersions.items.forEach(function(plugin) {
  pluginVersionsSupported.forEach(function(supportedPlugin) {
    if (plugin.uniqueIdentifier === supportedPlugin.uniqueIdentifier) {
      if (plugin.version == supportedPlugin.version) {
        pluginData = {
          uniqueIdentifier: plugin.uniqueIdentifier,
          version: []// need to update this to be an array
        }
      }
    }
  })

I appreciate all the help.

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

0

you need to use Array.reduce method:

const data = 
  [ { uniqueIdentifier: 'theme_test', version: '2011120800' } 
  , { uniqueIdentifier: 'theme_test', version: '3011120800' } 
  , { uniqueIdentifier: 'theme_test', version: '4011120800' } 
  ] 
const result = Object.values(data.reduce( (r,{uniqueIdentifier,version}) =>
  {
  r[uniqueIdentifier] ??= { uniqueIdentifier, version:[] }
  r[uniqueIdentifier].version.push(version)
  return r
  },{}))

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Assuming you only have one pluginData: move the new object out of the loop so that it doesn't get created repeatedly, and in the loop push the version to the existing array.

pluginData = {
  uniqueIdentifier: plugin.uniqueIdentifier,
  version: []
}
item.pluginVersions.items.forEach(function(plugin) {
  ...
      if (plugin.version == supportedPlugin.version) {
        pluginData.version.push(plugin.version);
about 4 years ago · Juan Pablo Isaza Report

0

You may also use Array#reduce() and Object.entries() as follows:

const data = [{"uniqueIdentifier": "theme_test","version": "2011120800"},{"uniqueIdentifier": "theme_test","version": "3011120800"},{"uniqueIdentifier": "theme_test","version": "4011120800"}];

const groupedData = Object.entries(
    data.reduce(
        (prev, {uniqueIdentifier,version}) => 
        ({ ...prev, [uniqueIdentifier]:(prev[uniqueIdentifier] || []).concat(version) }), {}
    )
)
.map(([uniqueIdentifier,version]) => ({uniqueIdentifier,version}));

console.log( groupedData );

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!