javascript - Array to Object with Values in a Range -
i have crazy requirement here. not sure how make work well, may thought of getting community's here. first time here me. let me explain situation clearly.
i have array of, 10 elements.
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reverse();
now need create object of keys containing array elements, values should like:
- first 5 "high".
- next 3 "medium".
- rest 2 "low".
i have above information in array:
var capacity = [5, 3, 2];
the above 3 values (high
, medium
, low) static. not sure how proceed here. not sure if should using for
loop hardcoded like:
var obj = {}; (var = 0; < capacity[0]; i++) obj[arr[i]] = "high"; (; < capacity[0] + capacity[1]; i++) obj[arr[i]] = "medium"; (; < capacity[0] + capacity[1] + capacity[2]; i++) obj[arr[i]] = "low";
not sure if right way proceed. pointers right direction? thanks.
snippet
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reverse(); var capacity = [5, 3, 2]; var obj = {}; (var = 0; < capacity[0]; i++) obj[arr[i]] = "high"; (; < capacity[0] + capacity[1]; i++) obj[arr[i]] = "medium"; (; < capacity[0] + capacity[1] + capacity[2]; i++) obj[arr[i]] = "low"; console.log(obj);
Comments
Post a Comment