0

I have a javascript that I am using to pull id of a tag . But due to some limitation on a platform where I need to use this I have to make this script under function so I will get the output in return. I am trying but I am failing to get it.

This is the script I have;

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

This is what I am trying to come up;

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);

}

This is the test url -> https://jsfiddle.net/v3d0nz9d/

I need to get output in return as ['123456', '7896669', '1147777']

Any help would be highly appreciated. TIA

1 Answer 1

1

[].map is shorthand for Array.prototype.map, so you don't need to store that in a variable, you can just return the return value of your map. Anything after a return statement won't be called, so your console.log is never reached. Also, to log the values returned from your function, you need to call it in your 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());
Sign up to request clarification or add additional context in comments.

1 Comment

ah okay I got it @Lee thank you for fixing my script. Really appreciate your help. thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.