javascript - Push array into array with keys -
i working arrays , pushing items array. below have loop, inside casperjs invoked , links scrapped , placed in array. page links placed in array named page_links
, video links in array video_links
. trying merge both arrays 1 array. how can push items in array keys?
var page_links = []; var video_links = []; (var = 0; < categoryurls.length; i++) { // start loop casper.thenopen(categoryurls[i], function() { tryandscroll(this); casper.then(function() { this.getelementsinfo('.title').foreach(function(element) { // skip elements don't have href attribute... if (!element.attributes.href) { return; } page_links.push( element.attributes.href ); casper.thenopen(element.attributes.href, function() { this.click('.responsivewrapper'); }).then(function(){ casper.each(this.getelementsinfo('.badge-youtube-player'), function(casper, element, j) { video_links.push( element["attributes"]["src"] ); }); }); }); }); }); }
desired result
{ { 'page_link' : 'www.example.com', 'video_link' : 'www.example.com' }, { 'page_link' : 'www.example.com', 'video_link' : 'www.example.com' } }
considering arrays as, can obtain means i'm not considering here -
var page_links = [ "link1", "link2" ]; var video_links = [ "vlink1", "vlink2" ];
also assuming length of both arrays same (you can apply logic check this, i've omitted here), here solutions
var finalarr = []; for(var ii = 0, ll = page_links.length; ii < ll; ii++) { finalarr[ii] = {}; finalarr[ii].page_link = page_links[ii]; finalarr[ii].video_link = video_links[ii]; }
thie finalarr
updated following array details
[{page_link: "link1", video_link: "vlink1"}, ...];
Comments
Post a Comment