0

I'm using npmscraper. I have two nearly identical functions below, which i want to basically combine the results they return into one array in key-value pair format.

First function returns ['orange','apple','grape']

Second function returns ['www.orange.com','www.apple.com','www.grape.com']


(very simplified) sample data to scrape from foo.com ###

<p>orange <a href="www.orange.com">click here</a></p>
<p>apple <a href="www.apple.com">click here</a></p>
<p>grape <a href="www.graphe.com">click here</a></p>

// Begin node app
 var scraperjs = require('scraperjs');

 // first function
  scraperjs.StaticScraper.create('https://foo.com/')
    .scrape(function($) {
        return $(".entry p").map(function() {
              return = $(this).text(); 
       }).get();
    })
    .then(function(fruit) { 
        // after some cleaning up...
        console.log(fruit)
         //returns ['orange','apple','grape']
    })

-----------------------
 // second function gets the links
 scraperjs.StaticScraper.create('https://foo.com/')
    .scrape(function($) {
        return $(".entry a").map(function() {
              return = $(this).attr('href'); 
       }).get();
    })
    .then(function(links) {
        console.log(links)
         // returns ['www.orange.com','www.apple.com','www.grape.com']
    })

(EDITED) What I'd like is something like:

[{fruit: 'orange'; link 'www.orange.com'},{fruit: 'apple'; link 'www.apple.com'}]
1
  • What I'd like is something like this is not a valid javascript construct ... did you mean the [] to be {} ... return = ??? really? do you claim your code to actually produce the current output? Commented Aug 30, 2017 at 1:46

1 Answer 1

1

so, you'll have two arrays

var array1 = ['orange','apple','grape'];
var array2 = ['www.orange.com','www.apple.com','www.grape.com']

// combining them to create an object

var result = array1.reduce(function(obj, key, index) {
    obj[key] = array2[index];
    return obj;
}, {});
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.