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

135
Views
How to conbine array to object in an array in javascript

I got two array here:

let arr1 = [
  {
    sockets: 0,
    implicitMods: [
      '+2 to Level of Socketed AoE Gems',
      '+2 to Level of Socketed Trap or Mine Gems'
    ],
    explicitMods: [], // arr2[0] in here
  },
  {
    sockets: 3,
    implicitMods: [ 'Secrets of Suffering' ],
    explicitMods: [], // arr2[1] in here
  }
]

let arr2 = [
  [
    [ 'explicit.stat_2866361420' ],
    [ 'explicit.stat_1978899297' ],
    [ 'explicit.stat_2290031712' ],
    [ 'explicit.stat_4294267596' ]
  ],
  [
    [ 'explicit.stat_2974417149' ],
    [ 'explicit.stat_3556824919' ],
    [ 'explicit.stat_789117908' ],
    [ 'explicit.stat_124131830' ],
    [ 'explicit.stat_1600707273' ],
    [ 'explicit.stat_3742945352' ]
  ]
]

i want put arr2[0] to arr1's first explicitMods and arr2[1] to arr1's second explicitMods, how i can get object order in array (e.g.arr1[0]==={first object}, arr1[1]==={second object})? and how to achieve my goal?

i tried to use

arr1.map(el=>el.explicitMods = arr2)

and result is explicitMods: [[Array], [Array]]

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You may try to create two for loops like this:

//For loop that starts from 0 to the length of the input array(arr2)
for(let i=0; i<arr2.length; i++){
//Save the expMods in i position temporally
    let expMods = arr1[i].explicitMods;
    //Make a loop for the first element of the arr2 which is also an array
    for(let j=0; j<arr2[i].length; j++){
    //Get the arr2 at current i pos and push into expMods the arr2[i][j]
       let temp = arr2[i];
       expMods.push(temp[j]);
    }
}

I've tested into the console and it worked, let me know

about 4 years ago · Santiago Trujillo Report

0

const arr1 = [
  {
    sockets: 0,
    implicitMods: [
      '+2 to Level of Socketed AoE Gems',
      '+2 to Level of Socketed Trap or Mine Gems'
    ],
    explicitMods: [], // arr2[0] in here
  },
  {
    sockets: 3,
    implicitMods: [ 'Secrets of Suffering' ],
    explicitMods: [], // arr2[1] in here
  }
]

const arr2 = [
  [
    [ 'explicit.stat_2866361420' ],
    [ 'explicit.stat_1978899297' ],
    [ 'explicit.stat_2290031712' ],
    [ 'explicit.stat_4294267596' ]
  ],
  [
    [ 'explicit.stat_2974417149' ],
    [ 'explicit.stat_3556824919' ],
    [ 'explicit.stat_789117908' ],
    [ 'explicit.stat_124131830' ],
    [ 'explicit.stat_1600707273' ],
    [ 'explicit.stat_3742945352' ]
  ]
];

for (let i = 0; i < arr1.length; i++) {
  const item1 = arr1[i];
  const item2 = arr2[i];
  //item1.explicitMods.push(...item2);
  item1.explicitMods.push(...item2.flat());
}

console.log(arr1);

about 4 years ago · Santiago Trujillo Report

0

You could use map(), grab the index and use it to add the corresponding value.

let arr1 = [{ sockets: 0, implicitMods: ['+2 to Level of Socketed AoE Gems', '+2 to Level of Socketed Trap or Mine Gems'], explicitMods: [], }, { sockets: 3, implicitMods: ['Secrets of Suffering'], explicitMods: [], }];
let arr2 = [[['explicit.stat_2866361420'], ['explicit.stat_1978899297'], ['explicit.stat_2290031712'], ['explicit.stat_4294267596']], [['explicit.stat_2974417149'], ['explicit.stat_3556824919'], ['explicit.stat_789117908'], ['explicit.stat_124131830'], ['explicit.stat_1600707273'], ['explicit.stat_3742945352']]];

const result = arr1.map((v, i) => {
  return {
    ...v,
    explicitMods: arr2[i]
  }
});

console.log(result);

about 4 years ago · Santiago Trujillo 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!