0

在 CoffeeScript 中,我试图测试给定的字符串是否是字符串数组的成员。这是我尝试过的(请注意,第一次尝试使用in运算符,第二次尝试使用运算of符):

console.log list
list = [ i.courseid for i in list ]
console.log list
console.log courseid
console.log courseid in list
console.log '2904984' in list
console.log 2904984 in list

和:

console.log list
list = [ i.courseid for i in list ]
console.log list
console.log courseid
console.log courseid of list
console.log '2904984' of list
console.log 2904984 of list

编译和运行这两个脚本的结果是:

[ { courseid: '2904984', _id: 5243274d46f7f6d72e000001 },
  { courseid: '2484419', _id: 5245ac6ef3a459ee44000002 },
  { courseid: '2180094', _id: 5245b3fa659d2c2d45000001 },
  { courseid: '2179793', _id: 5245ca8ab405987a49000001 },
  { courseid: '2169240', _id: 5245ca8eb405987a49000002 } ]
2904984,2484419,2180094,2179793,2169240
2484419
false
false
false

当项目是列表的成员时为什么不返回item in listitem of list返回的任何线索?true

4

1 回答 1

1

list = [i.courseid for i in list]为您提供嵌套的数组数组(包含字符串 ID 数组的 1 项数组)。你想要list = (i.courseid for i in list),给你一个你期望的扁平的字符串数组。

于 2013-09-30T17:06:33.880 回答