Why does map function return undefined but console.log logs out?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to return matching proprieties of two arrays of objects. But I got undefined from map function.



let fruits1 = [
{id: 1, name: "apple"},
{id: 2, name: "dragon fruit"},
{id: 3, name: "banana"},
{id: 4, name: "kiwi"},
{id: 5, name: "pineapple"},
{id: 6, name: "watermelon"},
{id: 7, name: "pear"},
]
let fruits2 = [
{id: 7, name: "pear"},
{id: 10, name: "avocado"},
{id: 5, name: "pineapple"},
]

fruits1.forEach((fruit1) => {
fruits2.filter((fruit2) => {
return fruit1.name === fruit2.name;
}).map((newFruit) => {
//console.log(newFruit.name);
return newFruit.name;
})
})









share|improve this question


















  • 5





    The .map() function returns a new array, but your code does not use the return value.

    – Pointy
    Jan 3 at 16:21






  • 2





    also Foreach returns undeifed developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Code Maniac
    Jan 3 at 16:22













  • fruits2.filter(o=> fruits1.some(i=> i.name == o.name) ).map(o=> o.name);

    – George Bailey
    Jan 3 at 16:27






  • 1





    Is it just me, or is it a slight code smell that the elements have an "id", but that's not what is being used in the comparison? Maybe not.

    – Taplar
    Jan 3 at 16:27


















0















I want to return matching proprieties of two arrays of objects. But I got undefined from map function.



let fruits1 = [
{id: 1, name: "apple"},
{id: 2, name: "dragon fruit"},
{id: 3, name: "banana"},
{id: 4, name: "kiwi"},
{id: 5, name: "pineapple"},
{id: 6, name: "watermelon"},
{id: 7, name: "pear"},
]
let fruits2 = [
{id: 7, name: "pear"},
{id: 10, name: "avocado"},
{id: 5, name: "pineapple"},
]

fruits1.forEach((fruit1) => {
fruits2.filter((fruit2) => {
return fruit1.name === fruit2.name;
}).map((newFruit) => {
//console.log(newFruit.name);
return newFruit.name;
})
})









share|improve this question


















  • 5





    The .map() function returns a new array, but your code does not use the return value.

    – Pointy
    Jan 3 at 16:21






  • 2





    also Foreach returns undeifed developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Code Maniac
    Jan 3 at 16:22













  • fruits2.filter(o=> fruits1.some(i=> i.name == o.name) ).map(o=> o.name);

    – George Bailey
    Jan 3 at 16:27






  • 1





    Is it just me, or is it a slight code smell that the elements have an "id", but that's not what is being used in the comparison? Maybe not.

    – Taplar
    Jan 3 at 16:27














0












0








0








I want to return matching proprieties of two arrays of objects. But I got undefined from map function.



let fruits1 = [
{id: 1, name: "apple"},
{id: 2, name: "dragon fruit"},
{id: 3, name: "banana"},
{id: 4, name: "kiwi"},
{id: 5, name: "pineapple"},
{id: 6, name: "watermelon"},
{id: 7, name: "pear"},
]
let fruits2 = [
{id: 7, name: "pear"},
{id: 10, name: "avocado"},
{id: 5, name: "pineapple"},
]

fruits1.forEach((fruit1) => {
fruits2.filter((fruit2) => {
return fruit1.name === fruit2.name;
}).map((newFruit) => {
//console.log(newFruit.name);
return newFruit.name;
})
})









share|improve this question














I want to return matching proprieties of two arrays of objects. But I got undefined from map function.



let fruits1 = [
{id: 1, name: "apple"},
{id: 2, name: "dragon fruit"},
{id: 3, name: "banana"},
{id: 4, name: "kiwi"},
{id: 5, name: "pineapple"},
{id: 6, name: "watermelon"},
{id: 7, name: "pear"},
]
let fruits2 = [
{id: 7, name: "pear"},
{id: 10, name: "avocado"},
{id: 5, name: "pineapple"},
]

fruits1.forEach((fruit1) => {
fruits2.filter((fruit2) => {
return fruit1.name === fruit2.name;
}).map((newFruit) => {
//console.log(newFruit.name);
return newFruit.name;
})
})






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 16:20









IvanIvan

65117




65117








  • 5





    The .map() function returns a new array, but your code does not use the return value.

    – Pointy
    Jan 3 at 16:21






  • 2





    also Foreach returns undeifed developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Code Maniac
    Jan 3 at 16:22













  • fruits2.filter(o=> fruits1.some(i=> i.name == o.name) ).map(o=> o.name);

    – George Bailey
    Jan 3 at 16:27






  • 1





    Is it just me, or is it a slight code smell that the elements have an "id", but that's not what is being used in the comparison? Maybe not.

    – Taplar
    Jan 3 at 16:27














  • 5





    The .map() function returns a new array, but your code does not use the return value.

    – Pointy
    Jan 3 at 16:21






  • 2





    also Foreach returns undeifed developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Code Maniac
    Jan 3 at 16:22













  • fruits2.filter(o=> fruits1.some(i=> i.name == o.name) ).map(o=> o.name);

    – George Bailey
    Jan 3 at 16:27






  • 1





    Is it just me, or is it a slight code smell that the elements have an "id", but that's not what is being used in the comparison? Maybe not.

    – Taplar
    Jan 3 at 16:27








5




5





The .map() function returns a new array, but your code does not use the return value.

– Pointy
Jan 3 at 16:21





The .map() function returns a new array, but your code does not use the return value.

– Pointy
Jan 3 at 16:21




2




2





also Foreach returns undeifed developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Code Maniac
Jan 3 at 16:22







also Foreach returns undeifed developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Code Maniac
Jan 3 at 16:22















fruits2.filter(o=> fruits1.some(i=> i.name == o.name) ).map(o=> o.name);

– George Bailey
Jan 3 at 16:27





fruits2.filter(o=> fruits1.some(i=> i.name == o.name) ).map(o=> o.name);

– George Bailey
Jan 3 at 16:27




1




1





Is it just me, or is it a slight code smell that the elements have an "id", but that's not what is being used in the comparison? Maybe not.

– Taplar
Jan 3 at 16:27





Is it just me, or is it a slight code smell that the elements have an "id", but that's not what is being used in the comparison? Maybe not.

– Taplar
Jan 3 at 16:27












3 Answers
3






active

oldest

votes


















0














What are you looking for is an array intersection:



// Generic helper function that can be used for the three operations:        
const operation = (list1, list2, isUnion = false) =>
list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

// Following functions are to be used:
const inBoth = (list1, list2) => operation(list1, list2, true),
inFirstOnly = operation,
inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);


Usage:



console.log('inBoth:', inBoth(list1, list2)); 


Working Example:






// Generic helper function that can be used for the three operations:        
const operation = (list1, list2, isUnion = false) =>
list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

// Following functions are to be used:
const inBoth = (list1, list2) => operation(list1, list2, true),
inFirstOnly = operation,
inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

let fruits1 = [
{id: 1, name: "apple"},
{id: 2, name: "dragon fruit"},
{id: 3, name: "banana"},
{id: 4, name: "kiwi"},
{id: 5, name: "pineapple"},
{id: 6, name: "watermelon"},
{id: 7, name: "pear"},
]
let fruits2 = [
{id: 7, name: "pear"},
{id: 10, name: "avocado"},
{id: 5, name: "pineapple"},
]

console.log('inBoth:', inBoth(fruits1, fruits2));








share|improve this answer































    0














    You could use a Set and filter the names.






    const names = ({ name }) => name;

    var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
    fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
    common = fruits1
    .map(names)
    .filter(Set.prototype.has, new Set(fruits2.map(names)));

    console.log(common);








    share|improve this answer































      0














      What you want to do is this:



      /* first we filter fruits1 (arbitrary) */
      let matchingFruits = fruits1.filter(f1 => {
      /* then we filter the frut if it exists in frtuis2 */
      return fruits2.find(f2 => f2.name === f1.name)
      }).map(fruit => fruit.name) // and now we map if we only want the name strings


      If you're not using a polyfill Array.find will not work in IE. The alternative would be using Array.indexOf (thanks for pointing this out @JakobE).



      Be aware that Array.forEach return value is undefined and that, in order to actually use the Array.map correctly, one has to consume the returned value somehow or assign it to a variable, as we just did with matchingFruits.






      share|improve this answer


























      • Note! 'find' is not supported by IE

        – Jakob E
        Jan 3 at 16:34











      • let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

        – Jakob E
        Jan 3 at 16:36











      • @JakobE just added that clarification, thanks.

        – GMaiolo
        Jan 3 at 17:03












      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026087%2fwhy-does-map-function-return-undefined-but-console-log-logs-out%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      What are you looking for is an array intersection:



      // Generic helper function that can be used for the three operations:        
      const operation = (list1, list2, isUnion = false) =>
      list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

      // Following functions are to be used:
      const inBoth = (list1, list2) => operation(list1, list2, true),
      inFirstOnly = operation,
      inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);


      Usage:



      console.log('inBoth:', inBoth(list1, list2)); 


      Working Example:






      // Generic helper function that can be used for the three operations:        
      const operation = (list1, list2, isUnion = false) =>
      list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

      // Following functions are to be used:
      const inBoth = (list1, list2) => operation(list1, list2, true),
      inFirstOnly = operation,
      inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

      let fruits1 = [
      {id: 1, name: "apple"},
      {id: 2, name: "dragon fruit"},
      {id: 3, name: "banana"},
      {id: 4, name: "kiwi"},
      {id: 5, name: "pineapple"},
      {id: 6, name: "watermelon"},
      {id: 7, name: "pear"},
      ]
      let fruits2 = [
      {id: 7, name: "pear"},
      {id: 10, name: "avocado"},
      {id: 5, name: "pineapple"},
      ]

      console.log('inBoth:', inBoth(fruits1, fruits2));








      share|improve this answer




























        0














        What are you looking for is an array intersection:



        // Generic helper function that can be used for the three operations:        
        const operation = (list1, list2, isUnion = false) =>
        list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

        // Following functions are to be used:
        const inBoth = (list1, list2) => operation(list1, list2, true),
        inFirstOnly = operation,
        inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);


        Usage:



        console.log('inBoth:', inBoth(list1, list2)); 


        Working Example:






        // Generic helper function that can be used for the three operations:        
        const operation = (list1, list2, isUnion = false) =>
        list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

        // Following functions are to be used:
        const inBoth = (list1, list2) => operation(list1, list2, true),
        inFirstOnly = operation,
        inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

        let fruits1 = [
        {id: 1, name: "apple"},
        {id: 2, name: "dragon fruit"},
        {id: 3, name: "banana"},
        {id: 4, name: "kiwi"},
        {id: 5, name: "pineapple"},
        {id: 6, name: "watermelon"},
        {id: 7, name: "pear"},
        ]
        let fruits2 = [
        {id: 7, name: "pear"},
        {id: 10, name: "avocado"},
        {id: 5, name: "pineapple"},
        ]

        console.log('inBoth:', inBoth(fruits1, fruits2));








        share|improve this answer


























          0












          0








          0







          What are you looking for is an array intersection:



          // Generic helper function that can be used for the three operations:        
          const operation = (list1, list2, isUnion = false) =>
          list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

          // Following functions are to be used:
          const inBoth = (list1, list2) => operation(list1, list2, true),
          inFirstOnly = operation,
          inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);


          Usage:



          console.log('inBoth:', inBoth(list1, list2)); 


          Working Example:






          // Generic helper function that can be used for the three operations:        
          const operation = (list1, list2, isUnion = false) =>
          list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

          // Following functions are to be used:
          const inBoth = (list1, list2) => operation(list1, list2, true),
          inFirstOnly = operation,
          inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

          let fruits1 = [
          {id: 1, name: "apple"},
          {id: 2, name: "dragon fruit"},
          {id: 3, name: "banana"},
          {id: 4, name: "kiwi"},
          {id: 5, name: "pineapple"},
          {id: 6, name: "watermelon"},
          {id: 7, name: "pear"},
          ]
          let fruits2 = [
          {id: 7, name: "pear"},
          {id: 10, name: "avocado"},
          {id: 5, name: "pineapple"},
          ]

          console.log('inBoth:', inBoth(fruits1, fruits2));








          share|improve this answer













          What are you looking for is an array intersection:



          // Generic helper function that can be used for the three operations:        
          const operation = (list1, list2, isUnion = false) =>
          list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

          // Following functions are to be used:
          const inBoth = (list1, list2) => operation(list1, list2, true),
          inFirstOnly = operation,
          inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);


          Usage:



          console.log('inBoth:', inBoth(list1, list2)); 


          Working Example:






          // Generic helper function that can be used for the three operations:        
          const operation = (list1, list2, isUnion = false) =>
          list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

          // Following functions are to be used:
          const inBoth = (list1, list2) => operation(list1, list2, true),
          inFirstOnly = operation,
          inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

          let fruits1 = [
          {id: 1, name: "apple"},
          {id: 2, name: "dragon fruit"},
          {id: 3, name: "banana"},
          {id: 4, name: "kiwi"},
          {id: 5, name: "pineapple"},
          {id: 6, name: "watermelon"},
          {id: 7, name: "pear"},
          ]
          let fruits2 = [
          {id: 7, name: "pear"},
          {id: 10, name: "avocado"},
          {id: 5, name: "pineapple"},
          ]

          console.log('inBoth:', inBoth(fruits1, fruits2));








          // Generic helper function that can be used for the three operations:        
          const operation = (list1, list2, isUnion = false) =>
          list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

          // Following functions are to be used:
          const inBoth = (list1, list2) => operation(list1, list2, true),
          inFirstOnly = operation,
          inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

          let fruits1 = [
          {id: 1, name: "apple"},
          {id: 2, name: "dragon fruit"},
          {id: 3, name: "banana"},
          {id: 4, name: "kiwi"},
          {id: 5, name: "pineapple"},
          {id: 6, name: "watermelon"},
          {id: 7, name: "pear"},
          ]
          let fruits2 = [
          {id: 7, name: "pear"},
          {id: 10, name: "avocado"},
          {id: 5, name: "pineapple"},
          ]

          console.log('inBoth:', inBoth(fruits1, fruits2));





          // Generic helper function that can be used for the three operations:        
          const operation = (list1, list2, isUnion = false) =>
          list1.filter( a => isUnion === list2.some( b => a.name === b.name ) );

          // Following functions are to be used:
          const inBoth = (list1, list2) => operation(list1, list2, true),
          inFirstOnly = operation,
          inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);

          let fruits1 = [
          {id: 1, name: "apple"},
          {id: 2, name: "dragon fruit"},
          {id: 3, name: "banana"},
          {id: 4, name: "kiwi"},
          {id: 5, name: "pineapple"},
          {id: 6, name: "watermelon"},
          {id: 7, name: "pear"},
          ]
          let fruits2 = [
          {id: 7, name: "pear"},
          {id: 10, name: "avocado"},
          {id: 5, name: "pineapple"},
          ]

          console.log('inBoth:', inBoth(fruits1, fruits2));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 16:56









          Mosè RaguzziniMosè Raguzzini

          4,7701226




          4,7701226

























              0














              You could use a Set and filter the names.






              const names = ({ name }) => name;

              var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
              fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
              common = fruits1
              .map(names)
              .filter(Set.prototype.has, new Set(fruits2.map(names)));

              console.log(common);








              share|improve this answer




























                0














                You could use a Set and filter the names.






                const names = ({ name }) => name;

                var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
                fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
                common = fruits1
                .map(names)
                .filter(Set.prototype.has, new Set(fruits2.map(names)));

                console.log(common);








                share|improve this answer


























                  0












                  0








                  0







                  You could use a Set and filter the names.






                  const names = ({ name }) => name;

                  var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
                  fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
                  common = fruits1
                  .map(names)
                  .filter(Set.prototype.has, new Set(fruits2.map(names)));

                  console.log(common);








                  share|improve this answer













                  You could use a Set and filter the names.






                  const names = ({ name }) => name;

                  var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
                  fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
                  common = fruits1
                  .map(names)
                  .filter(Set.prototype.has, new Set(fruits2.map(names)));

                  console.log(common);








                  const names = ({ name }) => name;

                  var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
                  fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
                  common = fruits1
                  .map(names)
                  .filter(Set.prototype.has, new Set(fruits2.map(names)));

                  console.log(common);





                  const names = ({ name }) => name;

                  var fruits1 = [{ id: 1, name: "apple" }, { id: 2, name: "dragon fruit" }, { id: 3, name: "banana" }, { id: 4, name: "kiwi" }, { id: 5, name: "pineapple" }, { id: 6, name: "watermelon" }, { id: 7, name: "pear" }],
                  fruits2 = [{ id: 7, name: "pear" }, { id: 10, name: "avocado" }, { id: 5, name: "pineapple" }],
                  common = fruits1
                  .map(names)
                  .filter(Set.prototype.has, new Set(fruits2.map(names)));

                  console.log(common);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 17:00









                  Nina ScholzNina Scholz

                  198k15110181




                  198k15110181























                      0














                      What you want to do is this:



                      /* first we filter fruits1 (arbitrary) */
                      let matchingFruits = fruits1.filter(f1 => {
                      /* then we filter the frut if it exists in frtuis2 */
                      return fruits2.find(f2 => f2.name === f1.name)
                      }).map(fruit => fruit.name) // and now we map if we only want the name strings


                      If you're not using a polyfill Array.find will not work in IE. The alternative would be using Array.indexOf (thanks for pointing this out @JakobE).



                      Be aware that Array.forEach return value is undefined and that, in order to actually use the Array.map correctly, one has to consume the returned value somehow or assign it to a variable, as we just did with matchingFruits.






                      share|improve this answer


























                      • Note! 'find' is not supported by IE

                        – Jakob E
                        Jan 3 at 16:34











                      • let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

                        – Jakob E
                        Jan 3 at 16:36











                      • @JakobE just added that clarification, thanks.

                        – GMaiolo
                        Jan 3 at 17:03
















                      0














                      What you want to do is this:



                      /* first we filter fruits1 (arbitrary) */
                      let matchingFruits = fruits1.filter(f1 => {
                      /* then we filter the frut if it exists in frtuis2 */
                      return fruits2.find(f2 => f2.name === f1.name)
                      }).map(fruit => fruit.name) // and now we map if we only want the name strings


                      If you're not using a polyfill Array.find will not work in IE. The alternative would be using Array.indexOf (thanks for pointing this out @JakobE).



                      Be aware that Array.forEach return value is undefined and that, in order to actually use the Array.map correctly, one has to consume the returned value somehow or assign it to a variable, as we just did with matchingFruits.






                      share|improve this answer


























                      • Note! 'find' is not supported by IE

                        – Jakob E
                        Jan 3 at 16:34











                      • let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

                        – Jakob E
                        Jan 3 at 16:36











                      • @JakobE just added that clarification, thanks.

                        – GMaiolo
                        Jan 3 at 17:03














                      0












                      0








                      0







                      What you want to do is this:



                      /* first we filter fruits1 (arbitrary) */
                      let matchingFruits = fruits1.filter(f1 => {
                      /* then we filter the frut if it exists in frtuis2 */
                      return fruits2.find(f2 => f2.name === f1.name)
                      }).map(fruit => fruit.name) // and now we map if we only want the name strings


                      If you're not using a polyfill Array.find will not work in IE. The alternative would be using Array.indexOf (thanks for pointing this out @JakobE).



                      Be aware that Array.forEach return value is undefined and that, in order to actually use the Array.map correctly, one has to consume the returned value somehow or assign it to a variable, as we just did with matchingFruits.






                      share|improve this answer















                      What you want to do is this:



                      /* first we filter fruits1 (arbitrary) */
                      let matchingFruits = fruits1.filter(f1 => {
                      /* then we filter the frut if it exists in frtuis2 */
                      return fruits2.find(f2 => f2.name === f1.name)
                      }).map(fruit => fruit.name) // and now we map if we only want the name strings


                      If you're not using a polyfill Array.find will not work in IE. The alternative would be using Array.indexOf (thanks for pointing this out @JakobE).



                      Be aware that Array.forEach return value is undefined and that, in order to actually use the Array.map correctly, one has to consume the returned value somehow or assign it to a variable, as we just did with matchingFruits.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 3 at 17:03

























                      answered Jan 3 at 16:30









                      GMaioloGMaiolo

                      1,9031024




                      1,9031024













                      • Note! 'find' is not supported by IE

                        – Jakob E
                        Jan 3 at 16:34











                      • let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

                        – Jakob E
                        Jan 3 at 16:36











                      • @JakobE just added that clarification, thanks.

                        – GMaiolo
                        Jan 3 at 17:03



















                      • Note! 'find' is not supported by IE

                        – Jakob E
                        Jan 3 at 16:34











                      • let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

                        – Jakob E
                        Jan 3 at 16:36











                      • @JakobE just added that clarification, thanks.

                        – GMaiolo
                        Jan 3 at 17:03

















                      Note! 'find' is not supported by IE

                      – Jakob E
                      Jan 3 at 16:34





                      Note! 'find' is not supported by IE

                      – Jakob E
                      Jan 3 at 16:34













                      let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

                      – Jakob E
                      Jan 3 at 16:36





                      let matchingFruits = fruits1.filter(item => fruits2.map(item => item.name).indexOf(item.name)!== -1).map(item => item.name);

                      – Jakob E
                      Jan 3 at 16:36













                      @JakobE just added that clarification, thanks.

                      – GMaiolo
                      Jan 3 at 17:03





                      @JakobE just added that clarification, thanks.

                      – GMaiolo
                      Jan 3 at 17:03


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026087%2fwhy-does-map-function-return-undefined-but-console-log-logs-out%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      ts Property 'filter' does not exist on type '{}'

                      mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window