使用map.call返回一个对象

时间:2017-04-23 01:47:36

标签: javascript

这有效:

evaluate(function() {
    return Array.prototype.map.call(document.querySelectorAll('.container .title'), function(e) {
        return e.querySelector('.title').textContent;
    });
}
evaluate(function() {
    return Array.prototype.map.call(document.querySelectorAll('.container .title a'), function(e) {
        return e.querySelector('.title a').href;
    });
}
evaluate(function() {
    return Array.prototype.map.call(document.querySelectorAll('.container img'), function(e) {
        return e.querySelector('img').src;
    });
}

..但是,我想将所有结果作为单个对象返回,但这不起作用,因为我无法弄清楚如何获取索引:

evaluate(function() {
    return Array.prototype.map.call(document.querySelectorAll('.container'), function(e) {
        var data = {};
        data[index].title = e.querySelector('.title').textContent;
        data[index].link = e.querySelector('.title').href;
        data[index].image = e.querySelector('img').src;
        // return all results in a single object with each containing title, link and image
        return data; 
    });
}

这是完整的功能:

new Nightmare(
        .goto('https://www.example.com')
        .insert('form[id="gbqf"] input[id="gbqfq"]', 'keyword here')
        .click('form[id="gbqf"] button[id="gbqfb"]')
        .wait('.sh-sr__shop-result-group')
        .visible('.sh-sr__shop-result-group')
        .evaluate(function() {
            return Array.prototype.map.call(document.querySelectorAll('.sh-sr__shop-result-group .psli'), function(data, e) {
                data.push({
                    title: e.querySelector('.pslmain h3').textContent,
                    thumb: e.querySelector('.pslimg img').src,
                    link: e.querySelector('.pslmain h3 a').href,
                    price: e.querySelector('.pslline .price').textContent,
                    stars: e.querySelector('span._Ezj div').getAttribute('aria-label'),
                    ratings: e.querySelector('a.shop__secondary.sh-rt__product').textContent,
                    //features: e.querySelector('._Lqc .shop__secondary _MAj').innerHTML,
                    //description: e.querySelector('._Lqc').textContent
                });
                return data;
            });
        })
        .end()
        .then(function(result) {
            console.log(result);
        })
        .catch(function(error) {
            console.error('Search failed:', error);
        });

2 个答案:

答案 0 :(得分:0)

您可以在地图之前迭代document.querySelectorAll(' .container')并使用索引构建数组,例如:

var kvArray = [{index:0, value:document.querySelectorAll('.container')[0]}, {index:2, value:20}, {index:3, value: 30}];
var reformattedArray = kvArray.map(function(obj){ 
   var rObj = {};
   rObj[obj.key] = obj.valor;
   return rObj;
});

并使用您的对象键访问rObj通讯地点。

无论如何,从文档来看,似乎可以从回调函数中获取索引。请查看:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map?v=control

var new_array = arr.map(callback[, thisArg])
Parameters

callback
Function that produces an element of the new Array, taking three arguments:

currentValue
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array map was called upon.
thisArg
Optional. Value to use as this when executing callback.

希望它有所帮助。

答案 1 :(得分:0)

我不确定你想要的最终形象是什么形状。但这似乎尽可能接近第一次实施。

function evaluate(containerName){
  return Array.prototype.reduce.call(document.querySelectorAll(containerName), function(acc , e){
    acc.push({
      title: e.querySelector('.title').textContent, 
      link: e.querySelector('.title a').href,
      image: e.querySelector('img').src
    }) 
    return acc 
  } , [] )
}

返回与容器索引对应的obj数组。