最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - TypeError: obj[key].includes is not a function when filtering values of an object in an array - Stack Overflow

matteradmin6PV0评论

I wanted to filter the values in an array of objects by a certain value. When I run the function, I get TypeError: obj[key].includes is not a function. I am using reactjs. What I'm I missing in the function?

var arr = [{
 name: 'xyz',
 grade: 'x'
}, {
 name: 'yaya',
 grade: 'x'
}, {
 name: 'x',
 frade: 'd'
}, {
  name: 'a',
  grade: 'b'
}];

filterIt(arr, searchKey) {
  return arr.filter(obj => Object.keys(obj)
    .map(key => obj[key].includes(searchKey)));
}

I got this example from and tried it out

I wanted to filter the values in an array of objects by a certain value. When I run the function, I get TypeError: obj[key].includes is not a function. I am using reactjs. What I'm I missing in the function?

var arr = [{
 name: 'xyz',
 grade: 'x'
}, {
 name: 'yaya',
 grade: 'x'
}, {
 name: 'x',
 frade: 'd'
}, {
  name: 'a',
  grade: 'b'
}];

filterIt(arr, searchKey) {
  return arr.filter(obj => Object.keys(obj)
    .map(key => obj[key].includes(searchKey)));
}

I got this example from https://stackoverflow./a/40890687/5256509 and tried it out

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Jan 12, 2017 at 16:23 HanmaslahHanmaslah 7461 gold badge8 silver badges14 bronze badges 14
  • so what is obj[key] ? Sounds like it is not what you think it is. – epascarello Commented Jan 12, 2017 at 16:25
  • What's obj? Are all of the properties of it objects with an includes method? – Heretic Monkey Commented Jan 12, 2017 at 16:25
  • obj[key] is the value of the key passed @epascarello – Hanmaslah Commented Jan 12, 2017 at 16:28
  • What is it when it fails.... Not seeing an the array/objects your question is basically impossible to answer. – epascarello Commented Jan 12, 2017 at 16:29
  • @epascarello I have updated the question – Hanmaslah Commented Jan 12, 2017 at 16:32
 |  Show 9 more ments

1 Answer 1

Reset to default 3

You can't filter array of object like this, because this bination

Object.keys(obj).map(key => obj[key].includes(searchKey))

always provides an array (with true and false values), and any array is truthy. Hence filter doesn't filter anything. You can try something like this:

arr.filter(obj => 
   Object.keys(obj)
     .some(key => obj[key].includes(searchKey))
);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far