Home > Software engineering >  How to conbine array to object in an array in javascript
How to conbine array to object in an array in javascript

Time:04-23

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?

thanks

CodePudding user response:

try:

arr1.map((el, index)=> el.explicitMods = arr2[index])

CodePudding user response:

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

CodePudding user response:

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);

CodePudding user response:

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: [], // 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' ]
  ]
]

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

console.log(result);

  • Related