1

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]]

6
  • 2
    What have you tried and what was the result? Commented Apr 23, 2022 at 10:07
  • 1
    i tried to use arr1.map(el=>el.explicitMods = arr2) and result is explicitMods: [[Array], [Array]] @Yogi Commented Apr 23, 2022 at 10:12
  • How much research effort is expected of Stack Overflow users? Commented Apr 23, 2022 at 10:18
  • @Andreas sorry im just start learning this, i tried to google it, i dont konw what should i descript my question to goole. i'll try my best next time before asking question here. thx Commented Apr 23, 2022 at 10:29
  • You need to know how to work with arrays (access elements, store values, iterate over the elements). That's part of every JS tutorial. So you should at least be able to show a loop that tries to access/store something -> How much research effort is expected of Stack Overflow users? Commented Apr 23, 2022 at 10:39

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

try:

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

2 Comments

forEach is more appropriate here. (don't use map() unless you need the returned array)
Your answer could be improved by adding more information on what the code does and how it helps the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.