1

I run into a problem wherein I want to divide my items in by array likes this

[ 
 1 [ 1[[1][2]]  2[[3][4][5]] ],  2[ 1[[1][2]]  2[[3][4][5]]],  
 3 [ 1[[1][2]] 2[[3][4][5]]],  4[ 1[[1][2]]  2[[3][4][5]]]  
]

So basically in the first 1,2,3,4 array are the object array of the arrays... It can basically express like this

  • Array parrent [ 1 2 3 4 ]
    • Second array parent [ 1 2 ]
      • First third array [ 1 2 ]
      • Second third array [ 3 4 5 ]

In my code it is something like this

    for (let i = 1;i < 4 ;i++) {
        const list = []
        for (let j = 1; j < 16*i ; j++) {
            var list2 = []
            if (j % 5 == 0) {
                const list3 = []
                const list4 = []
                for (let k = 0; k < j; k++) {
                    if (k <= 1) {
                        list3.push(`/kgfpics/kgf${k}.png`)
                    } else if ( k >= 4) {
                        list4.push(`/kgfpics/kgf${k}.png`)
                    }
                    list2.push(list3,list4)
                }
                list.push(list2)
            }
        }
        // 1 [ 1 [ [1],[2] ] , 2 [ [3],[4],[5] ] ]
        topiclist.push(list)  
    }
    console.log(topiclist)

but the expectation result that I want to happen is not working...Can you notice where I am wrong here? Cause I wanna done my problem for this my project. Thank you very much.

5
  • So is it basically the numbers in your example arrays are the k values you are pushing image paths for? Commented May 7, 2022 at 4:13
  • Yes in third arrays..because I am really confuse of its arranging the array with simultaneous const array brackets. Commented May 7, 2022 at 4:15
  • why not use a Map()? Commented May 7, 2022 at 4:32
  • How..I'm not super known in map so I use for loop hehe... Commented May 7, 2022 at 4:34
  • I'd recommend to also checkout Graph Data structure.. maps are basically named arrays that also allows you to use map.get(key) similar to an object, it's more control in the long run. specially if you're working with typed arrays Commented May 7, 2022 at 4:40

1 Answer 1

1

Hopefully I've understood your desired result. You could hardcode the keys/indices you want to map until the most inner loop where they are finally mapped to the path values.

let k = 1;
const res = Array.from({ length: 4 }).map(() =>
  Array.from({ length: 2 }).map(() =>
    [2, 3].map((length) =>
      Array.from({ length }).map(() => `/kgfpics/kgf${k++}.png`)
    )
  )
);

console.log(res);

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

6 Comments

I am sorry sir.. I mean like I want to be that kind of arrangement I want but how do I get all the values from 1 - 45 numbers cause I have 45 images so and arrange them like how you do.. Its not like [1,2,3,4] It is just their separation same as the [1,2] and [3,4,5] .
the appending array should be like in from 1 - 45 so that I can get all my 45 pictures. but with that kind of arrangement in array.
@MYTH Ah, I see, ok, let me test another version.
Oh i already get it sir thanks. I just have to put a Array.from({length: 3}) in your codes.. Thank you.
last question sir can i use this in my advance animation? like anime.js or tweenmax,js or timelinemax.js or other javascript animation stuffs?
|

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.