Creating a JavaScript Object from two arrays

后端 未结 9 878
北海茫月
北海茫月 2020-11-28 06:17

I have two arrays: newParamArr[i] and paramVal[i].

Example values in the newParamArr[i] array: [\"Name\", \"Age\", \"Ema

9条回答
  •  旧巷少年郎
    2020-11-28 07:17

    I needed this in a few places so I made this function...

    function zip(arr1,arr2,out={}){
        arr1.map( (val,idx)=>{ out[val] = arr2[idx]; } );
        return out;
    }
    
    
    console.log( zip( ["a","b","c"], [1,2,3] ) );
    
    > {'a': 1, 'b': 2, 'c': 3} 
    

提交回复
热议问题