最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Array.includes always false javascript - Stack Overflow

programmeradmin2浏览0评论

I have the following object

{ _id: 58ae39c54473f9cfd4eb29b6,
  email: '[email protected]',
  name: 'Another Club Renamed',
  __v: 0,
  createdEvents: [],
  instructors: [ 58a5ac4f84230a2117d1d6cb, 111111111111111111111111 ],
  members: [] }

when I use the method

object.instructors.includes(58a5ac4f84230a2117d1d6cb) object.instructors.includes(111111111111111111111111)

or

object.instructors.includes('58a5ac4f84230a2117d1d6cb') object.instructors.includes("111111111111111111111111")

the result is always false am I doing something incorrect?

Array.isArray(club.instructors) returns true

EDIT: The object is a mongoose.document. The object above is exactly how it gets logged to the console when I execute

Club.findById(id (err, club) =>{
    console.log(club);
})

I have the following object

{ _id: 58ae39c54473f9cfd4eb29b6,
  email: '[email protected]',
  name: 'Another Club Renamed',
  __v: 0,
  createdEvents: [],
  instructors: [ 58a5ac4f84230a2117d1d6cb, 111111111111111111111111 ],
  members: [] }

when I use the method

object.instructors.includes(58a5ac4f84230a2117d1d6cb) object.instructors.includes(111111111111111111111111)

or

object.instructors.includes('58a5ac4f84230a2117d1d6cb') object.instructors.includes("111111111111111111111111")

the result is always false am I doing something incorrect?

Array.isArray(club.instructors) returns true

EDIT: The object is a mongoose.document. The object above is exactly how it gets logged to the console when I execute

Club.findById(id (err, club) =>{
    console.log(club);
})
Share Improve this question edited Feb 23, 2017 at 17:16 xerotolerant asked Feb 23, 2017 at 17:04 xerotolerantxerotolerant 2,0794 gold badges25 silver badges45 bronze badges 6
  • 2 The code as posted is syntactically incorrect; 58a5ac4f84230a2117d1d6cb is not a valid number. – Pointy Commented Feb 23, 2017 at 17:06
  • var x = [58a5ac4f84230a2117d1d6cb, 111111] which results in a syntax error ► var x = [2222,1111] is valid and so is ► var x = ['58a5ac4f84230a2117d1d6cb',111111111111111111111111] – Nope Commented Feb 23, 2017 at 17:07
  • What data type is 58a5ac4f84230a2117d1d6cb? – Redmega Commented Feb 23, 2017 at 17:09
  • it is mongoose.Schema.Types.ObjectId. That is how it is when I log it to console from a mongoose query. – xerotolerant Commented Feb 23, 2017 at 17:10
  • On a second note, how did you not notice a SyntaxError: Unexpected identifier at any stage? – Nope Commented Feb 23, 2017 at 17:10
 |  Show 1 more comment

4 Answers 4

Reset to default 7

the mongo _id is 12-byte BSON type ObjectId,

So normal object.instructors.includes('58a5ac4f84230a2117d1d6cb') means you are comparing a raw string to an ObjectId,which is not a valid integer or string, which will always return FALSE.

NOTE: Tips for further understanding


use lodash to check the same ObjectId stored in multiple collections

npm i lodash --save

in your file.js

const _ = require('lodash');

lodash _.isEqual performs a deep comparison between two values to determine if they are equivalent

use lodash and perform _.isEqual for deep comparing

_.isEqual(object._id, '58ae39c54473f9cfd4eb29b6') // false
_.isEqual(object.instructor[0], '58a5ac4f84230a2117d1d6cb') // false

suppose the id 58ae39c54473f9cfd4eb29b6 comes from two different mongodb documents

collection USERS: [{_id: 58ae39c54473f9cfd4eb29b6, name: 'user1' }] suppose this user is referenced to rooms collection:

collection ROOMS: [{_id: ObjectId, name: 'room1', userId: 58ae39c54473f9cfd4eb29b6}]

if you do a normal comparison=== or includes like this

if (user[0]._id === rooms[0].userId) // false due to different instances

_.isEqual(user[0]._id, rooms[0].userId) // true performs deep comparison

As Pointy pointed out (hehe), the value you're using on the array, 58a5ac4f84230a2117d1d6cb, is not a valid number. It's possible your object is not being defined properly due to that invalidity, and thus everything will return false, because nothing is defined.

I've tested on object.instructors.includes(111111111111111111111111), and it works. I believe your problem is on 58a5ac4f84230a2117d1d6cb, which if you really want to keep, should be enclosed in quotes to become a string.

If you're using includes in js loops take care there's a tricky note here between for...in loops and traditional loops. check snippet ;)

A for...in loop only iterates over enumerable properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.prototype, such as String's indexOf() method or Object's toString() method.

The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).

Reference: Mozilla Web docs for javascript

var array = [1, 2, 3, 4, 5]

console.log('** using for..in loops i actually is String')

for (var i in array)
    console.log(array.includes(i))
    
console.log('** using traditional for loops i is integer')

for (var i = 0; i < array.length; i++)
    console.log(array.includes(i))

If you are doing a search operator, according to first result of the search you can try this...

let { ...x} = Array1;

console.log(Array2.map((a) => a.field).includes(x[0].field))
发布评论

评论列表(0)

  1. 暂无评论