Javascript, repeating an object key N-times, being N its value

后端 未结 6 1000
有刺的猬
有刺的猬 2020-12-20 01:11

I was wondering how to do this in the more cleaner and optimal way:

I have an Object with the following structure:

{
   \"125\": 2,
   \"439\": 3,
           


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 01:35

    Hmm... not sure if I got at, but maybe something like this:

    var myObj = {
       "125": 2,
       "439": 3,
       "560": 1,
       "999": 2
    },
    myArray = [];
    
    for(k in myObj){
        for(i = 0; i < myObj[k]; i++){
            myArray.push(k);
        }
    }
    console.log(myArray)
    

提交回复
热议问题