lodash filter in array












0















in lodash there is a possibility to filter within an array which is in an object?



I have an object that has an array in it. It looks like this



{
"id": "1",
"name": "Test 1",
"tag": ["blue","red", "yellow"]
},
{
"id": "2",
"name": "Test 2",
"tag": ["red", "yellow"]
},
{
"id": "3",
"name": "Test 3",
"tag": ["green"]
}


What I want to do now.
If the tag is Red he should output the object with the ids: 1 and 2. Tag = Green only the object with the id: 3. And so on.



I have now tried to solve this with the lodash filter.



 const filteredColors = _.filter(colors, function(c) {
return _.includes(['Test 1', 'Test 2'], c.name);
});
// returns Objects with 2 Entrys = Correct


I can filter normal values, but how can I find the value in the array?










share|improve this question



























    0















    in lodash there is a possibility to filter within an array which is in an object?



    I have an object that has an array in it. It looks like this



    {
    "id": "1",
    "name": "Test 1",
    "tag": ["blue","red", "yellow"]
    },
    {
    "id": "2",
    "name": "Test 2",
    "tag": ["red", "yellow"]
    },
    {
    "id": "3",
    "name": "Test 3",
    "tag": ["green"]
    }


    What I want to do now.
    If the tag is Red he should output the object with the ids: 1 and 2. Tag = Green only the object with the id: 3. And so on.



    I have now tried to solve this with the lodash filter.



     const filteredColors = _.filter(colors, function(c) {
    return _.includes(['Test 1', 'Test 2'], c.name);
    });
    // returns Objects with 2 Entrys = Correct


    I can filter normal values, but how can I find the value in the array?










    share|improve this question

























      0












      0








      0








      in lodash there is a possibility to filter within an array which is in an object?



      I have an object that has an array in it. It looks like this



      {
      "id": "1",
      "name": "Test 1",
      "tag": ["blue","red", "yellow"]
      },
      {
      "id": "2",
      "name": "Test 2",
      "tag": ["red", "yellow"]
      },
      {
      "id": "3",
      "name": "Test 3",
      "tag": ["green"]
      }


      What I want to do now.
      If the tag is Red he should output the object with the ids: 1 and 2. Tag = Green only the object with the id: 3. And so on.



      I have now tried to solve this with the lodash filter.



       const filteredColors = _.filter(colors, function(c) {
      return _.includes(['Test 1', 'Test 2'], c.name);
      });
      // returns Objects with 2 Entrys = Correct


      I can filter normal values, but how can I find the value in the array?










      share|improve this question














      in lodash there is a possibility to filter within an array which is in an object?



      I have an object that has an array in it. It looks like this



      {
      "id": "1",
      "name": "Test 1",
      "tag": ["blue","red", "yellow"]
      },
      {
      "id": "2",
      "name": "Test 2",
      "tag": ["red", "yellow"]
      },
      {
      "id": "3",
      "name": "Test 3",
      "tag": ["green"]
      }


      What I want to do now.
      If the tag is Red he should output the object with the ids: 1 and 2. Tag = Green only the object with the id: 3. And so on.



      I have now tried to solve this with the lodash filter.



       const filteredColors = _.filter(colors, function(c) {
      return _.includes(['Test 1', 'Test 2'], c.name);
      });
      // returns Objects with 2 Entrys = Correct


      I can filter normal values, but how can I find the value in the array?







      javascript lodash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 11:05









      CkappoCkappo

      106111




      106111
























          5 Answers
          5






          active

          oldest

          votes


















          2














          There's no need for lodash, just check if the tag array includes what you're looking for:






          const arr = [{
          "id": "1",
          "name": "Test 1",
          "tag": ["blue","red", "yellow"]
          },
          {
          "id": "2",
          "name": "Test 2",
          "tag": ["red", "yellow"]
          },
          {
          "id": "3",
          "name": "Test 3",
          "tag": ["green"]
          }];
          console.log(
          arr.filter(({ tag }) => tag.includes('red'))
          );








          share|improve this answer































            0














            I have solved it with:



             let filter = _.filter(
            colors,
            _.flow(
            _.property('tag'),
            _.partial(_.intersection, ['red', 'green']),
            _.size,
            ),
            );





            share|improve this answer































              0














              Convert your array to String, and then you can check string is contain your colour or not. Like.



               const items =  [
              {
              "id": "1",
              "name": "Test 1",
              "tag": ["blue","red", "yellow"]
              },
              {
              "id": "2",
              "name": "Test 2",
              "tag": ["red", "yellow"]
              },
              {
              "id": "3",
              "name": "Test 3",
              "tag": ["green"]
              }]

              function findColorId(color){
              return items.filter((d)=> {
              if(String(d.tag).includes(color)){
              return d;
              }
              });
              }

              findColorId('red');





              share|improve this answer































                0














                You can use Array.prototype.filter():




                The filter() method creates a new array with all elements that pass the test implemented by the provided function.




                and Array.prototype.map():




                The map() method creates a new array with the results of calling a provided function on every element in the calling array.







                const colors = [{
                "id": "1",
                "name": "Test 1",
                "tag": ["blue","red", "yellow"]
                },
                {
                "id": "2",
                "name": "Test 2",
                "tag": ["red", "yellow"]
                },
                {
                "id": "3",
                "name": "Test 3",
                "tag": ["green"]
                }]

                function filteredColors(colorsArr, c){
                return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                }
                console.log(filteredColors(colors, 'red'));
                console.log(filteredColors(colors, 'green'));








                share|improve this answer

































                  0














                  This in lodash is somewhat longer than with ES6.






                  const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                  // lodash
                  const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                  // ES6
                  const es6 = c => data.filter(x => x.tag.includes(c))

                  console.log(lodash('green'))
                  console.log(es6('green'))

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                  The idea in both is to simply use _.filter / Array.filter and then _.includes / Array.includes






                  share|improve this answer

























                    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%2f53391626%2flodash-filter-in-array%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    2














                    There's no need for lodash, just check if the tag array includes what you're looking for:






                    const arr = [{
                    "id": "1",
                    "name": "Test 1",
                    "tag": ["blue","red", "yellow"]
                    },
                    {
                    "id": "2",
                    "name": "Test 2",
                    "tag": ["red", "yellow"]
                    },
                    {
                    "id": "3",
                    "name": "Test 3",
                    "tag": ["green"]
                    }];
                    console.log(
                    arr.filter(({ tag }) => tag.includes('red'))
                    );








                    share|improve this answer




























                      2














                      There's no need for lodash, just check if the tag array includes what you're looking for:






                      const arr = [{
                      "id": "1",
                      "name": "Test 1",
                      "tag": ["blue","red", "yellow"]
                      },
                      {
                      "id": "2",
                      "name": "Test 2",
                      "tag": ["red", "yellow"]
                      },
                      {
                      "id": "3",
                      "name": "Test 3",
                      "tag": ["green"]
                      }];
                      console.log(
                      arr.filter(({ tag }) => tag.includes('red'))
                      );








                      share|improve this answer


























                        2












                        2








                        2







                        There's no need for lodash, just check if the tag array includes what you're looking for:






                        const arr = [{
                        "id": "1",
                        "name": "Test 1",
                        "tag": ["blue","red", "yellow"]
                        },
                        {
                        "id": "2",
                        "name": "Test 2",
                        "tag": ["red", "yellow"]
                        },
                        {
                        "id": "3",
                        "name": "Test 3",
                        "tag": ["green"]
                        }];
                        console.log(
                        arr.filter(({ tag }) => tag.includes('red'))
                        );








                        share|improve this answer













                        There's no need for lodash, just check if the tag array includes what you're looking for:






                        const arr = [{
                        "id": "1",
                        "name": "Test 1",
                        "tag": ["blue","red", "yellow"]
                        },
                        {
                        "id": "2",
                        "name": "Test 2",
                        "tag": ["red", "yellow"]
                        },
                        {
                        "id": "3",
                        "name": "Test 3",
                        "tag": ["green"]
                        }];
                        console.log(
                        arr.filter(({ tag }) => tag.includes('red'))
                        );








                        const arr = [{
                        "id": "1",
                        "name": "Test 1",
                        "tag": ["blue","red", "yellow"]
                        },
                        {
                        "id": "2",
                        "name": "Test 2",
                        "tag": ["red", "yellow"]
                        },
                        {
                        "id": "3",
                        "name": "Test 3",
                        "tag": ["green"]
                        }];
                        console.log(
                        arr.filter(({ tag }) => tag.includes('red'))
                        );





                        const arr = [{
                        "id": "1",
                        "name": "Test 1",
                        "tag": ["blue","red", "yellow"]
                        },
                        {
                        "id": "2",
                        "name": "Test 2",
                        "tag": ["red", "yellow"]
                        },
                        {
                        "id": "3",
                        "name": "Test 3",
                        "tag": ["green"]
                        }];
                        console.log(
                        arr.filter(({ tag }) => tag.includes('red'))
                        );






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 20 '18 at 11:06









                        CertainPerformanceCertainPerformance

                        82.9k144067




                        82.9k144067

























                            0














                            I have solved it with:



                             let filter = _.filter(
                            colors,
                            _.flow(
                            _.property('tag'),
                            _.partial(_.intersection, ['red', 'green']),
                            _.size,
                            ),
                            );





                            share|improve this answer




























                              0














                              I have solved it with:



                               let filter = _.filter(
                              colors,
                              _.flow(
                              _.property('tag'),
                              _.partial(_.intersection, ['red', 'green']),
                              _.size,
                              ),
                              );





                              share|improve this answer


























                                0












                                0








                                0







                                I have solved it with:



                                 let filter = _.filter(
                                colors,
                                _.flow(
                                _.property('tag'),
                                _.partial(_.intersection, ['red', 'green']),
                                _.size,
                                ),
                                );





                                share|improve this answer













                                I have solved it with:



                                 let filter = _.filter(
                                colors,
                                _.flow(
                                _.property('tag'),
                                _.partial(_.intersection, ['red', 'green']),
                                _.size,
                                ),
                                );






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 20 '18 at 11:13









                                CkappoCkappo

                                106111




                                106111























                                    0














                                    Convert your array to String, and then you can check string is contain your colour or not. Like.



                                     const items =  [
                                    {
                                    "id": "1",
                                    "name": "Test 1",
                                    "tag": ["blue","red", "yellow"]
                                    },
                                    {
                                    "id": "2",
                                    "name": "Test 2",
                                    "tag": ["red", "yellow"]
                                    },
                                    {
                                    "id": "3",
                                    "name": "Test 3",
                                    "tag": ["green"]
                                    }]

                                    function findColorId(color){
                                    return items.filter((d)=> {
                                    if(String(d.tag).includes(color)){
                                    return d;
                                    }
                                    });
                                    }

                                    findColorId('red');





                                    share|improve this answer




























                                      0














                                      Convert your array to String, and then you can check string is contain your colour or not. Like.



                                       const items =  [
                                      {
                                      "id": "1",
                                      "name": "Test 1",
                                      "tag": ["blue","red", "yellow"]
                                      },
                                      {
                                      "id": "2",
                                      "name": "Test 2",
                                      "tag": ["red", "yellow"]
                                      },
                                      {
                                      "id": "3",
                                      "name": "Test 3",
                                      "tag": ["green"]
                                      }]

                                      function findColorId(color){
                                      return items.filter((d)=> {
                                      if(String(d.tag).includes(color)){
                                      return d;
                                      }
                                      });
                                      }

                                      findColorId('red');





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Convert your array to String, and then you can check string is contain your colour or not. Like.



                                         const items =  [
                                        {
                                        "id": "1",
                                        "name": "Test 1",
                                        "tag": ["blue","red", "yellow"]
                                        },
                                        {
                                        "id": "2",
                                        "name": "Test 2",
                                        "tag": ["red", "yellow"]
                                        },
                                        {
                                        "id": "3",
                                        "name": "Test 3",
                                        "tag": ["green"]
                                        }]

                                        function findColorId(color){
                                        return items.filter((d)=> {
                                        if(String(d.tag).includes(color)){
                                        return d;
                                        }
                                        });
                                        }

                                        findColorId('red');





                                        share|improve this answer













                                        Convert your array to String, and then you can check string is contain your colour or not. Like.



                                         const items =  [
                                        {
                                        "id": "1",
                                        "name": "Test 1",
                                        "tag": ["blue","red", "yellow"]
                                        },
                                        {
                                        "id": "2",
                                        "name": "Test 2",
                                        "tag": ["red", "yellow"]
                                        },
                                        {
                                        "id": "3",
                                        "name": "Test 3",
                                        "tag": ["green"]
                                        }]

                                        function findColorId(color){
                                        return items.filter((d)=> {
                                        if(String(d.tag).includes(color)){
                                        return d;
                                        }
                                        });
                                        }

                                        findColorId('red');






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 20 '18 at 11:20









                                        Biplab MalakarBiplab Malakar

                                        40837




                                        40837























                                            0














                                            You can use Array.prototype.filter():




                                            The filter() method creates a new array with all elements that pass the test implemented by the provided function.




                                            and Array.prototype.map():




                                            The map() method creates a new array with the results of calling a provided function on every element in the calling array.







                                            const colors = [{
                                            "id": "1",
                                            "name": "Test 1",
                                            "tag": ["blue","red", "yellow"]
                                            },
                                            {
                                            "id": "2",
                                            "name": "Test 2",
                                            "tag": ["red", "yellow"]
                                            },
                                            {
                                            "id": "3",
                                            "name": "Test 3",
                                            "tag": ["green"]
                                            }]

                                            function filteredColors(colorsArr, c){
                                            return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                                            }
                                            console.log(filteredColors(colors, 'red'));
                                            console.log(filteredColors(colors, 'green'));








                                            share|improve this answer






























                                              0














                                              You can use Array.prototype.filter():




                                              The filter() method creates a new array with all elements that pass the test implemented by the provided function.




                                              and Array.prototype.map():




                                              The map() method creates a new array with the results of calling a provided function on every element in the calling array.







                                              const colors = [{
                                              "id": "1",
                                              "name": "Test 1",
                                              "tag": ["blue","red", "yellow"]
                                              },
                                              {
                                              "id": "2",
                                              "name": "Test 2",
                                              "tag": ["red", "yellow"]
                                              },
                                              {
                                              "id": "3",
                                              "name": "Test 3",
                                              "tag": ["green"]
                                              }]

                                              function filteredColors(colorsArr, c){
                                              return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                                              }
                                              console.log(filteredColors(colors, 'red'));
                                              console.log(filteredColors(colors, 'green'));








                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                You can use Array.prototype.filter():




                                                The filter() method creates a new array with all elements that pass the test implemented by the provided function.




                                                and Array.prototype.map():




                                                The map() method creates a new array with the results of calling a provided function on every element in the calling array.







                                                const colors = [{
                                                "id": "1",
                                                "name": "Test 1",
                                                "tag": ["blue","red", "yellow"]
                                                },
                                                {
                                                "id": "2",
                                                "name": "Test 2",
                                                "tag": ["red", "yellow"]
                                                },
                                                {
                                                "id": "3",
                                                "name": "Test 3",
                                                "tag": ["green"]
                                                }]

                                                function filteredColors(colorsArr, c){
                                                return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                                                }
                                                console.log(filteredColors(colors, 'red'));
                                                console.log(filteredColors(colors, 'green'));








                                                share|improve this answer















                                                You can use Array.prototype.filter():




                                                The filter() method creates a new array with all elements that pass the test implemented by the provided function.




                                                and Array.prototype.map():




                                                The map() method creates a new array with the results of calling a provided function on every element in the calling array.







                                                const colors = [{
                                                "id": "1",
                                                "name": "Test 1",
                                                "tag": ["blue","red", "yellow"]
                                                },
                                                {
                                                "id": "2",
                                                "name": "Test 2",
                                                "tag": ["red", "yellow"]
                                                },
                                                {
                                                "id": "3",
                                                "name": "Test 3",
                                                "tag": ["green"]
                                                }]

                                                function filteredColors(colorsArr, c){
                                                return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                                                }
                                                console.log(filteredColors(colors, 'red'));
                                                console.log(filteredColors(colors, 'green'));








                                                const colors = [{
                                                "id": "1",
                                                "name": "Test 1",
                                                "tag": ["blue","red", "yellow"]
                                                },
                                                {
                                                "id": "2",
                                                "name": "Test 2",
                                                "tag": ["red", "yellow"]
                                                },
                                                {
                                                "id": "3",
                                                "name": "Test 3",
                                                "tag": ["green"]
                                                }]

                                                function filteredColors(colorsArr, c){
                                                return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                                                }
                                                console.log(filteredColors(colors, 'red'));
                                                console.log(filteredColors(colors, 'green'));





                                                const colors = [{
                                                "id": "1",
                                                "name": "Test 1",
                                                "tag": ["blue","red", "yellow"]
                                                },
                                                {
                                                "id": "2",
                                                "name": "Test 2",
                                                "tag": ["red", "yellow"]
                                                },
                                                {
                                                "id": "3",
                                                "name": "Test 3",
                                                "tag": ["green"]
                                                }]

                                                function filteredColors(colorsArr, c){
                                                return colorsArr.filter(i => i.tag.includes(c)).map(i => ({id: i.id}));
                                                }
                                                console.log(filteredColors(colors, 'red'));
                                                console.log(filteredColors(colors, 'green'));






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 20 '18 at 11:52

























                                                answered Nov 20 '18 at 11:18









                                                MamunMamun

                                                26.4k71630




                                                26.4k71630























                                                    0














                                                    This in lodash is somewhat longer than with ES6.






                                                    const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                                                    // lodash
                                                    const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                                                    // ES6
                                                    const es6 = c => data.filter(x => x.tag.includes(c))

                                                    console.log(lodash('green'))
                                                    console.log(es6('green'))

                                                    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                                                    The idea in both is to simply use _.filter / Array.filter and then _.includes / Array.includes






                                                    share|improve this answer






























                                                      0














                                                      This in lodash is somewhat longer than with ES6.






                                                      const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                                                      // lodash
                                                      const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                                                      // ES6
                                                      const es6 = c => data.filter(x => x.tag.includes(c))

                                                      console.log(lodash('green'))
                                                      console.log(es6('green'))

                                                      <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                                                      The idea in both is to simply use _.filter / Array.filter and then _.includes / Array.includes






                                                      share|improve this answer




























                                                        0












                                                        0








                                                        0







                                                        This in lodash is somewhat longer than with ES6.






                                                        const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                                                        // lodash
                                                        const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                                                        // ES6
                                                        const es6 = c => data.filter(x => x.tag.includes(c))

                                                        console.log(lodash('green'))
                                                        console.log(es6('green'))

                                                        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                                                        The idea in both is to simply use _.filter / Array.filter and then _.includes / Array.includes






                                                        share|improve this answer















                                                        This in lodash is somewhat longer than with ES6.






                                                        const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                                                        // lodash
                                                        const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                                                        // ES6
                                                        const es6 = c => data.filter(x => x.tag.includes(c))

                                                        console.log(lodash('green'))
                                                        console.log(es6('green'))

                                                        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                                                        The idea in both is to simply use _.filter / Array.filter and then _.includes / Array.includes






                                                        const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                                                        // lodash
                                                        const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                                                        // ES6
                                                        const es6 = c => data.filter(x => x.tag.includes(c))

                                                        console.log(lodash('green'))
                                                        console.log(es6('green'))

                                                        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>





                                                        const data = [{ "id": "1", "name": "Test 1", "tag": ["blue","red", "yellow"] }, { "id": "2", "name": "Test 2", "tag": ["red", "yellow"] }, { "id": "3", "name": "Test 3", "tag": ["green"] }]

                                                        // lodash
                                                        const lodash = c => _.filter(data, x => _.includes(x.tag,c))

                                                        // ES6
                                                        const es6 = c => data.filter(x => x.tag.includes(c))

                                                        console.log(lodash('green'))
                                                        console.log(es6('green'))

                                                        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>






                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Nov 20 '18 at 18:18

























                                                        answered Nov 20 '18 at 18:11









                                                        AkrionAkrion

                                                        9,41711224




                                                        9,41711224






























                                                            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%2f53391626%2flodash-filter-in-array%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

                                                            MongoDB - Not Authorized To Execute Command

                                                            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                                            Npm cannot find a required file even through it is in the searched directory