find element in array in array of objects












0














I'm trying to return the object that contains the value (any letter matching in any object) in search field.



I want to return the object that the array inside an array of object matches.



Example:



if




search = 'dog'




then I want to return




[{name: Jake, last: DK, tags: ['fun','dog','cat']}]




Or if




search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]




My attempt:



this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})


Also, I have




if (obj.tags !== undefined) {




because its a list that appears on click of a button and its undefined initially when page render.










share|improve this question
























  • .filter() always returns an array. And an array is truthy. Hence filteredTag (which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags is not undefined).
    – Andreas
    Nov 22 at 18:20












  • Even after your last edit, you still refer to student without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
    – Mark Adelsberger
    Nov 22 at 18:24










  • @MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
    – Binny.H
    Nov 22 at 18:25






  • 1




    What is your question?
    – Andy Ray
    Nov 22 at 18:26
















0














I'm trying to return the object that contains the value (any letter matching in any object) in search field.



I want to return the object that the array inside an array of object matches.



Example:



if




search = 'dog'




then I want to return




[{name: Jake, last: DK, tags: ['fun','dog','cat']}]




Or if




search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]




My attempt:



this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})


Also, I have




if (obj.tags !== undefined) {




because its a list that appears on click of a button and its undefined initially when page render.










share|improve this question
























  • .filter() always returns an array. And an array is truthy. Hence filteredTag (which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags is not undefined).
    – Andreas
    Nov 22 at 18:20












  • Even after your last edit, you still refer to student without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
    – Mark Adelsberger
    Nov 22 at 18:24










  • @MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
    – Binny.H
    Nov 22 at 18:25






  • 1




    What is your question?
    – Andy Ray
    Nov 22 at 18:26














0












0








0


0





I'm trying to return the object that contains the value (any letter matching in any object) in search field.



I want to return the object that the array inside an array of object matches.



Example:



if




search = 'dog'




then I want to return




[{name: Jake, last: DK, tags: ['fun','dog','cat']}]




Or if




search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]




My attempt:



this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})


Also, I have




if (obj.tags !== undefined) {




because its a list that appears on click of a button and its undefined initially when page render.










share|improve this question















I'm trying to return the object that contains the value (any letter matching in any object) in search field.



I want to return the object that the array inside an array of object matches.



Example:



if




search = 'dog'




then I want to return




[{name: Jake, last: DK, tags: ['fun','dog','cat']}]




Or if




search = 'a' return [{name: Jake, last: DK, tags: ['fun','dog','cat']},{name: John, last: Hop, tags: ['boring','mouse','trap']}]




My attempt:



this.state = {
search = '',
arr = [{name: Jake, last: DK, tags: ['fun','dog','cat']},
{name: John, last: Hop, tags: ['boring','mouse','trap']}]
}
//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.search)
})
)
}
})


Also, I have




if (obj.tags !== undefined) {




because its a list that appears on click of a button and its undefined initially when page render.







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 18:35









josh21

105312




105312










asked Nov 22 at 18:16









Binny.H

386




386












  • .filter() always returns an array. And an array is truthy. Hence filteredTag (which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags is not undefined).
    – Andreas
    Nov 22 at 18:20












  • Even after your last edit, you still refer to student without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
    – Mark Adelsberger
    Nov 22 at 18:24










  • @MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
    – Binny.H
    Nov 22 at 18:25






  • 1




    What is your question?
    – Andy Ray
    Nov 22 at 18:26


















  • .filter() always returns an array. And an array is truthy. Hence filteredTag (which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags is not undefined).
    – Andreas
    Nov 22 at 18:20












  • Even after your last edit, you still refer to student without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
    – Mark Adelsberger
    Nov 22 at 18:24










  • @MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
    – Binny.H
    Nov 22 at 18:25






  • 1




    What is your question?
    – Andy Ray
    Nov 22 at 18:26
















.filter() always returns an array. And an array is truthy. Hence filteredTag (which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags is not undefined).
– Andreas
Nov 22 at 18:20






.filter() always returns an array. And an array is truthy. Hence filteredTag (which btw. is a terrible name when the actual content is an array of students) will always have all students (when student.tags is not undefined).
– Andreas
Nov 22 at 18:20














Even after your last edit, you still refer to student without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
– Mark Adelsberger
Nov 22 at 18:24




Even after your last edit, you still refer to student without, as far as anyone can see, having defined it. (And also mention it in your second code fragment.)
– Mark Adelsberger
Nov 22 at 18:24












@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25




@MarkAdelsberger sorry I've just fixed all the typo's. I had originally copy pasted my code but was making it easier to read, but didn't notice all the typos.
– Binny.H
Nov 22 at 18:25




1




1




What is your question?
– Andy Ray
Nov 22 at 18:26




What is your question?
– Andy Ray
Nov 22 at 18:26












5 Answers
5






active

oldest

votes


















1














The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.



let filteredTag = this.state.arr.filter(obj => {
if (obj.tags !== undefined) {
return (
obj.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
}).length > 0 // since if tag has any element matching,filtered tags length can be checked
)
}
return false // since in undefined case,search condition is not met.
})





share|improve this answer























  • Follow up question, what if I wanted to also filter by first name or last name?
    – Binny.H
    Nov 22 at 19:00










  • I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
    – anuragb26
    Nov 23 at 6:17





















0














You need to return true or false in the callback function of filter. For example, student.tags.filter( has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => { does not return a boolean. Here's how it should be changed:



//asuming the arr is stored in state
let filteredTag = this.state.arr.filter(obj => {
if (obj.tags === undefined) return false;
var result = student.tags.filter(tag => {
return tag.includes(this.state.tagSearch)
});
return result.length > 0;
})





share|improve this answer





























    0














    You can use a combination of filter and some to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)):






    function search(term, arr) {
    return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
    }

    var data = [{
    name: 'Jake',
    last: 'DK',
    tags: ['fun', 'dog', 'cat']
    },
    {
    name: 'John',
    last: 'Hop',
    tags: ['boring', 'mouse', 'trap']
    }
    ];

    console.log(search('dog', data));
    console.log(search('a', data));
    console.log(search('dog', undefined));








    share|improve this answer































      0














      in this method, you return many elements that have the same tag:






      var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
      {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
      {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

      function search(searchText){
      var ret =
      arr.forEach(function (element){
      if(element.tags.includes(searchText)){
      ret.push(element)
      }
      })
      return ret;
      }

      console.log(search("dog"));








      share|improve this answer





























        0














        This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some() is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:






        let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
        {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

        let term = 'dog'
        let found = arr.filter(item =>
        /* return a boolean here */
        /* && will short circuit returning false if there's no tags */
        /* some will return true if any word contains term */
        item.tags && item.tags.some(word => word.includes(term))
        )
        console.log(found)

        // also works with `a`

        term = 'a'
        found = arr.filter(item =>
        item.tags && item.tags.some(word => word.includes(term))
        )
        console.log(found)








        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%2f53436367%2ffind-element-in-array-in-array-of-objects%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









          1














          The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.



          let filteredTag = this.state.arr.filter(obj => {
          if (obj.tags !== undefined) {
          return (
          obj.tags.filter(tag => {
          return tag.includes(this.state.tagSearch)
          }).length > 0 // since if tag has any element matching,filtered tags length can be checked
          )
          }
          return false // since in undefined case,search condition is not met.
          })





          share|improve this answer























          • Follow up question, what if I wanted to also filter by first name or last name?
            – Binny.H
            Nov 22 at 19:00










          • I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
            – anuragb26
            Nov 23 at 6:17


















          1














          The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.



          let filteredTag = this.state.arr.filter(obj => {
          if (obj.tags !== undefined) {
          return (
          obj.tags.filter(tag => {
          return tag.includes(this.state.tagSearch)
          }).length > 0 // since if tag has any element matching,filtered tags length can be checked
          )
          }
          return false // since in undefined case,search condition is not met.
          })





          share|improve this answer























          • Follow up question, what if I wanted to also filter by first name or last name?
            – Binny.H
            Nov 22 at 19:00










          • I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
            – anuragb26
            Nov 23 at 6:17
















          1












          1








          1






          The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.



          let filteredTag = this.state.arr.filter(obj => {
          if (obj.tags !== undefined) {
          return (
          obj.tags.filter(tag => {
          return tag.includes(this.state.tagSearch)
          }).length > 0 // since if tag has any element matching,filtered tags length can be checked
          )
          }
          return false // since in undefined case,search condition is not met.
          })





          share|improve this answer














          The return value of filter should be true/false depending on whether you want the particular element.With that in mind,you need to make following changes.



          let filteredTag = this.state.arr.filter(obj => {
          if (obj.tags !== undefined) {
          return (
          obj.tags.filter(tag => {
          return tag.includes(this.state.tagSearch)
          }).length > 0 // since if tag has any element matching,filtered tags length can be checked
          )
          }
          return false // since in undefined case,search condition is not met.
          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 18:34

























          answered Nov 22 at 18:25









          anuragb26

          16214




          16214












          • Follow up question, what if I wanted to also filter by first name or last name?
            – Binny.H
            Nov 22 at 19:00










          • I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
            – anuragb26
            Nov 23 at 6:17




















          • Follow up question, what if I wanted to also filter by first name or last name?
            – Binny.H
            Nov 22 at 19:00










          • I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
            – anuragb26
            Nov 23 at 6:17


















          Follow up question, what if I wanted to also filter by first name or last name?
          – Binny.H
          Nov 22 at 19:00




          Follow up question, what if I wanted to also filter by first name or last name?
          – Binny.H
          Nov 22 at 19:00












          I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
          – anuragb26
          Nov 23 at 6:17






          I assume you want the search item to be present in any of the following. 1.Included in tag of the tag list. 2.Included in the firstName 3.Included in the lastName. Add LGGICAL OR conditions with inner filter. obj.tags.filter(tag => { return tag.includes(this.state.tagSearch) }).length > 0 || obj.name.includes(this.state.tagSearch) || obj.last.includes(this.state.tagSearch)
          – anuragb26
          Nov 23 at 6:17















          0














          You need to return true or false in the callback function of filter. For example, student.tags.filter( has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => { does not return a boolean. Here's how it should be changed:



          //asuming the arr is stored in state
          let filteredTag = this.state.arr.filter(obj => {
          if (obj.tags === undefined) return false;
          var result = student.tags.filter(tag => {
          return tag.includes(this.state.tagSearch)
          });
          return result.length > 0;
          })





          share|improve this answer


























            0














            You need to return true or false in the callback function of filter. For example, student.tags.filter( has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => { does not return a boolean. Here's how it should be changed:



            //asuming the arr is stored in state
            let filteredTag = this.state.arr.filter(obj => {
            if (obj.tags === undefined) return false;
            var result = student.tags.filter(tag => {
            return tag.includes(this.state.tagSearch)
            });
            return result.length > 0;
            })





            share|improve this answer
























              0












              0








              0






              You need to return true or false in the callback function of filter. For example, student.tags.filter( has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => { does not return a boolean. Here's how it should be changed:



              //asuming the arr is stored in state
              let filteredTag = this.state.arr.filter(obj => {
              if (obj.tags === undefined) return false;
              var result = student.tags.filter(tag => {
              return tag.includes(this.state.tagSearch)
              });
              return result.length > 0;
              })





              share|improve this answer












              You need to return true or false in the callback function of filter. For example, student.tags.filter( has a correct callback function since it returns either true or false. However, this.state.arr.filter(obj => { does not return a boolean. Here's how it should be changed:



              //asuming the arr is stored in state
              let filteredTag = this.state.arr.filter(obj => {
              if (obj.tags === undefined) return false;
              var result = student.tags.filter(tag => {
              return tag.includes(this.state.tagSearch)
              });
              return result.length > 0;
              })






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 at 18:26









              Kevin Bai

              18817




              18817























                  0














                  You can use a combination of filter and some to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)):






                  function search(term, arr) {
                  return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
                  }

                  var data = [{
                  name: 'Jake',
                  last: 'DK',
                  tags: ['fun', 'dog', 'cat']
                  },
                  {
                  name: 'John',
                  last: 'Hop',
                  tags: ['boring', 'mouse', 'trap']
                  }
                  ];

                  console.log(search('dog', data));
                  console.log(search('a', data));
                  console.log(search('dog', undefined));








                  share|improve this answer




























                    0














                    You can use a combination of filter and some to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)):






                    function search(term, arr) {
                    return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
                    }

                    var data = [{
                    name: 'Jake',
                    last: 'DK',
                    tags: ['fun', 'dog', 'cat']
                    },
                    {
                    name: 'John',
                    last: 'Hop',
                    tags: ['boring', 'mouse', 'trap']
                    }
                    ];

                    console.log(search('dog', data));
                    console.log(search('a', data));
                    console.log(search('dog', undefined));








                    share|improve this answer


























                      0












                      0








                      0






                      You can use a combination of filter and some to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)):






                      function search(term, arr) {
                      return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
                      }

                      var data = [{
                      name: 'Jake',
                      last: 'DK',
                      tags: ['fun', 'dog', 'cat']
                      },
                      {
                      name: 'John',
                      last: 'Hop',
                      tags: ['boring', 'mouse', 'trap']
                      }
                      ];

                      console.log(search('dog', data));
                      console.log(search('a', data));
                      console.log(search('dog', undefined));








                      share|improve this answer














                      You can use a combination of filter and some to find all objects that have tags that "match" the search term (a match is defined as tag.includes(term)):






                      function search(term, arr) {
                      return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
                      }

                      var data = [{
                      name: 'Jake',
                      last: 'DK',
                      tags: ['fun', 'dog', 'cat']
                      },
                      {
                      name: 'John',
                      last: 'Hop',
                      tags: ['boring', 'mouse', 'trap']
                      }
                      ];

                      console.log(search('dog', data));
                      console.log(search('a', data));
                      console.log(search('dog', undefined));








                      function search(term, arr) {
                      return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
                      }

                      var data = [{
                      name: 'Jake',
                      last: 'DK',
                      tags: ['fun', 'dog', 'cat']
                      },
                      {
                      name: 'John',
                      last: 'Hop',
                      tags: ['boring', 'mouse', 'trap']
                      }
                      ];

                      console.log(search('dog', data));
                      console.log(search('a', data));
                      console.log(search('dog', undefined));





                      function search(term, arr) {
                      return (arr || ).filter(e => (e.tags || ).some(t => t.includes(term)));
                      }

                      var data = [{
                      name: 'Jake',
                      last: 'DK',
                      tags: ['fun', 'dog', 'cat']
                      },
                      {
                      name: 'John',
                      last: 'Hop',
                      tags: ['boring', 'mouse', 'trap']
                      }
                      ];

                      console.log(search('dog', data));
                      console.log(search('a', data));
                      console.log(search('dog', undefined));






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 at 18:33

























                      answered Nov 22 at 18:28









                      slider

                      8,0451129




                      8,0451129























                          0














                          in this method, you return many elements that have the same tag:






                          var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                          {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
                          {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

                          function search(searchText){
                          var ret =
                          arr.forEach(function (element){
                          if(element.tags.includes(searchText)){
                          ret.push(element)
                          }
                          })
                          return ret;
                          }

                          console.log(search("dog"));








                          share|improve this answer


























                            0














                            in this method, you return many elements that have the same tag:






                            var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                            {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
                            {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

                            function search(searchText){
                            var ret =
                            arr.forEach(function (element){
                            if(element.tags.includes(searchText)){
                            ret.push(element)
                            }
                            })
                            return ret;
                            }

                            console.log(search("dog"));








                            share|improve this answer
























                              0












                              0








                              0






                              in this method, you return many elements that have the same tag:






                              var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                              {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
                              {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

                              function search(searchText){
                              var ret =
                              arr.forEach(function (element){
                              if(element.tags.includes(searchText)){
                              ret.push(element)
                              }
                              })
                              return ret;
                              }

                              console.log(search("dog"));








                              share|improve this answer












                              in this method, you return many elements that have the same tag:






                              var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                              {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
                              {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

                              function search(searchText){
                              var ret =
                              arr.forEach(function (element){
                              if(element.tags.includes(searchText)){
                              ret.push(element)
                              }
                              })
                              return ret;
                              }

                              console.log(search("dog"));








                              var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                              {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
                              {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

                              function search(searchText){
                              var ret =
                              arr.forEach(function (element){
                              if(element.tags.includes(searchText)){
                              ret.push(element)
                              }
                              })
                              return ret;
                              }

                              console.log(search("dog"));





                              var arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                              {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']},
                              {name: 'Demian', last: 'Bibiano', tags: ['dog','cat','trap']}];

                              function search(searchText){
                              var ret =
                              arr.forEach(function (element){
                              if(element.tags.includes(searchText)){
                              ret.push(element)
                              }
                              })
                              return ret;
                              }

                              console.log(search("dog"));






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 at 18:36









                              Demian

                              1637




                              1637























                                  0














                                  This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some() is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:






                                  let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                                  {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

                                  let term = 'dog'
                                  let found = arr.filter(item =>
                                  /* return a boolean here */
                                  /* && will short circuit returning false if there's no tags */
                                  /* some will return true if any word contains term */
                                  item.tags && item.tags.some(word => word.includes(term))
                                  )
                                  console.log(found)

                                  // also works with `a`

                                  term = 'a'
                                  found = arr.filter(item =>
                                  item.tags && item.tags.some(word => word.includes(term))
                                  )
                                  console.log(found)








                                  share|improve this answer




























                                    0














                                    This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some() is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:






                                    let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                                    {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

                                    let term = 'dog'
                                    let found = arr.filter(item =>
                                    /* return a boolean here */
                                    /* && will short circuit returning false if there's no tags */
                                    /* some will return true if any word contains term */
                                    item.tags && item.tags.some(word => word.includes(term))
                                    )
                                    console.log(found)

                                    // also works with `a`

                                    term = 'a'
                                    found = arr.filter(item =>
                                    item.tags && item.tags.some(word => word.includes(term))
                                    )
                                    console.log(found)








                                    share|improve this answer


























                                      0












                                      0








                                      0






                                      This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some() is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:






                                      let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                                      {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

                                      let term = 'dog'
                                      let found = arr.filter(item =>
                                      /* return a boolean here */
                                      /* && will short circuit returning false if there's no tags */
                                      /* some will return true if any word contains term */
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)

                                      // also works with `a`

                                      term = 'a'
                                      found = arr.filter(item =>
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)








                                      share|improve this answer














                                      This would be simple if you just wanted to find the whole term. Since you want to find partial terms as well, you need to look at each tag. some() is really useful for that because you can set a condition and it reuturns a boolean, which is what you need here:






                                      let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                                      {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

                                      let term = 'dog'
                                      let found = arr.filter(item =>
                                      /* return a boolean here */
                                      /* && will short circuit returning false if there's no tags */
                                      /* some will return true if any word contains term */
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)

                                      // also works with `a`

                                      term = 'a'
                                      found = arr.filter(item =>
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)








                                      let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                                      {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

                                      let term = 'dog'
                                      let found = arr.filter(item =>
                                      /* return a boolean here */
                                      /* && will short circuit returning false if there's no tags */
                                      /* some will return true if any word contains term */
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)

                                      // also works with `a`

                                      term = 'a'
                                      found = arr.filter(item =>
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)





                                      let arr = [{name: 'Jake', last: 'DK', tags: ['fun','dog','cat']},
                                      {name: 'John', last: 'Hop', tags: ['boring','mouse','trap']}]

                                      let term = 'dog'
                                      let found = arr.filter(item =>
                                      /* return a boolean here */
                                      /* && will short circuit returning false if there's no tags */
                                      /* some will return true if any word contains term */
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)

                                      // also works with `a`

                                      term = 'a'
                                      found = arr.filter(item =>
                                      item.tags && item.tags.some(word => word.includes(term))
                                      )
                                      console.log(found)






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 22 at 18:43

























                                      answered Nov 22 at 18:31









                                      Mark Meyer

                                      35.1k32855




                                      35.1k32855






























                                          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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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%2f53436367%2ffind-element-in-array-in-array-of-objects%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

                                          What visual should I use to simply compare current year value vs last year in Power BI desktop

                                          How to ignore python UserWarning in pytest?

                                          Alexandru Averescu