Reputation: 664
I am currently working with arrays and pushing items into an array. Below I have for loop, inside CasperJS is invoked and links are scrapped and placed in array. Page links are placed in array named page_links
and video links in array video_links
. However I am trying to merge both arrays into one array. How can I push the items in array with keys?
var page_links = [];
var video_links = [];
for (var i = 0; i < categoryUrls.length; i++)
{ // start for loop
casper.thenOpen(categoryUrls[i], function() {
tryAndScroll(this);
casper.then(function() {
this.getElementsInfo('.title').forEach(function(element) {
// skip elements that don't have a 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' } }
Upvotes: 0
Views: 788
Reputation: 2759
Considering the arrays as, which you can obtain by any means which I'm not considering here -
var page_links = [ "Link1", "link2" ];
var video_links = [ "vlink1", "vlink2" ];
Also assuming length of both arrays are same (you can apply logic to check this, which I've omitted here), here is the 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
will be updated with following array details
[{page_link: "Link1", video_link: "vlink1"}, ...];
Upvotes: 1