javascript - ways to convert array to object in my case -
i face problem lots , tired of writing conversion function
i can do
function toobject(arr) { var rv = {}; (var = 0; < arr.length; ++i) rv[i] = arr[i]; return rv; }
but there short-cut that? case below :
angular.foreach($scope.data, function(item){ if(thread.checked === true){ var links = item.url; chrome.tabs.create(links, function(tab) { }); } });
i'm using chrome api links obj :
chrome.tabs.create(obj, function(tab) { });
in es5 browser can do:
var obj = {}; [0,1,2].foreach(function(v, i){obj[i] = v});
or
[0,1,2].foreach(function(v, i, arr){this[i] = v}, obj);
as function:
function toobj(arr) { var obj = {}; arr.foreach(function(v, i){obj[i] = v}); return obj; }
if object passed in may not array object properties 0 n, then:
function toobj(arr) { var obj = {}; [].foreach.call(arr, function(v, i){obj[i] = v}); return obj; }
Comments
Post a Comment