正在console.log中打印函数的输出

Printing output in console.log for a function

本文关键字:函数 输出 打印 console log 正在      更新时间:2023-09-26

我有一个javascript,用来提取标记的id。但由于平台上的一些限制,我需要使用它,我必须在函数下制作这个脚本,这样我才能得到输出作为回报。我在试,但没能拿到。

这是我的剧本;

var arr2st3 = [].map.call(document.querySelectorAll('article:nth-child(-n+4)'), function(el) {
    return el.id.replace(/[^'d]/g, '');
});

这就是我想要提出的;

function myfun() {
var arr2st3 = []
var arr2st3 = arr2st3.map.call(document.querySelectorAll('article:nth-child(-n+4)'), 
        function(el) {
        return el.id.replace(/[^'d]/g, '');
    });
    return;
        console.log(myfun);
}

这是测试url->https://jsfiddle.net/v3d0nz9d/

我需要得到[123456','7896669','1147777']的输出作为回报

如有任何帮助,我们将不胜感激。TIA

[].mapArray.prototype.map的简写,因此不需要将其存储在变量中,只需返回map的返回值即可。return语句之后的任何内容都不会被调用,因此永远无法访问您的console.log。此外,要记录函数返回的值,需要在console.log中调用它。

function myfun() {
  return [].map.call(document.querySelectorAll('article:nth-child(-n+4)'), function(el) {
    return el.id.replace(/[^'d]/g, '');
  });
}
console.log(myfun());