2

Can someone please explain how the below code working?

...

articles.map(createArticle).join("\n");

function createArticle(article){
  return `
    <div class="article">
      <h1>${article.news}</h1>
    </div>
  `
}

I understand how map(); works, I just don't understand where it gets article from as its not being passed as an argument to the createArticle function within map();

4
  • This is how .map function works: it invokes a specified callback for each element of an array, passing this element into that callback as a parameter. Commented Jun 15, 2018 at 9:20
  • article is the content of each item in the array, articles ... the callback "signature" is function(item, index, array) ... of course, in the callback function you can call those arguments what you like, in this case article and you don't have to use the 2nd and 3rd, which is the case Commented Jun 15, 2018 at 9:20
  • I don't get it ... you understand how map works ... but you don't actually understand how map works, because you don't know how the callback function is called - documentation may help Commented Jun 15, 2018 at 9:21
  • @Ollie Basically map is passing an object of articles to createArticle method that readout the news and join will concatenating the new with new line I have added a simple example below it will help to understand. Commented Jun 15, 2018 at 9:24

5 Answers 5

3

The createArticle function is actually being passed 3 arguments: the item in the array, the index of the item and the array.

articles.map(function createArticle(article, i, arr) {
    return `
        <div class="article">
            <h1>${article.news}</h1>
        </div>
    `
});

Your code is just changing the createArticle function from an anonymous function into a named one.

articles.map(createArticle);

function createArticle(article, i, arr) {
    return `
        <div class="article">
            <h1>${article.news}</h1>
        </div>
    `
}

Since parameters don't need to be declared in JavaScript, your code doesn't include the i or arr parameters.

articles.map(createArticle);

function createArticle(article) {
    return `
        <div class="article">
            <h1>${article.news}</h1>
        </div>
    `
}

You can see a full explaination and a polyfill (unnecessary these days, but can be helpful when trying to understand a function) on MDN

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

1 Comment

Thank you, this was very helpful
1

From the MDN docs:

map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results.

So, the argument refers to the each and every element; during the iteration.

Comments

1

createArticle is the callback called on each element of the articles array.

As explained in the doc, "callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed."

So here, article is the value of the current element being iterated on.

Comments

1

The function createArticle is passed as an argument to map.

The code for map (which you didn't write, is built into the JavaScript engine, and is not visible in the code you copy/pasted as a consequence of that) calls createArticle (which it has access to because you passed it as an argument). Since that code is calling it (once for each element in the array), it can pass createArticle whatever arguments it likes.

The arguments it passes are documented wherever map is documented, including:

Comments

-1
var persons = [
    {firstname : "Deepak", lastname: "Patidar"},      
    {firstname : "Anjular", lastname: "5"},
    {firstname : "Java", lastname: "script"}
];


function getFullName(item,index) {
    var fullname = [item.firstname,item.lastname].join(" ");
    return fullname;
}

function myFunction() { 
    document.getElementById("demo").innerHTML = persons.map(getFullName);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.