我有一个函数返回一个如下所示的数组:
.then(Cause.findOne(causeId).populate('admins').exec(function (err, cause) {
var ids = cause.admins.map(function(admin) {
return admin.id;
})
var join_ids = "'" + ids.join("','");
console.log(join_ids);
的输出'26c14292-a181-48bd-8344-73fa9caf65e7','64405c09-61d2-43ed-8b15-a99f92dff6e9','bdc034df-82f5-4cd8-a310-a3c3e2fe3106'
我试图将数组的第一个值作为userId过滤器传递给另一个函数:
let message = {
app_id: `${app_id}`,
contents: {"en": "Yeah Buddy," + Cause.name + "Rolling Like a Big Shot!"},
filters: [{'field': 'tag', 'key': 'userId', 'relation': '=', 'value': `${join_ids}`}]
和console.log(消息)的输出;
{ app_id: '*****************',
contents: { en: 'Yeah Buddy,undefinedRolling Like a Big Shot!' },
filters:
[ { field: 'tag',
key: 'userId',
relation: '=',
value: '\'26c14292-a181-48bd-8344-73fa9caf65e7\',\'64405c09-61d2-43ed-8b15-a99f92dff6e9\',\'bdc034df-82f5-4cd8-a310-a3c3e2fe3106' } ],
ios_badgeType: 'Increase',
ios_badgeCount: 1 }
如果我把console.log(join_ids [0]);
2
的console.log(消息);
{ app_id: '*****************',
contents: { en: 'Yeah Buddy,undefinedRolling Like a Big Shot!' },
filters:
[ { field: 'tag',
key: 'userId',
relation: '=',
value: 2} ],
ios_badgeType: 'Increase',
ios_badgeCount: 1 }
我的问题是如何将join_ids的输出变为索引为0,1,2,3的数组。
即。
join_ids[0] = '26c14292-a181-48bd-8344-73fa9caf65e7', join_ids[1] = '64405c09-61d2-43ed-8b15-a99f92dff6e9'
谢谢!
答案 0 :(得分:1)
基于您的第二个代码段,它看起来像已经是一个数组。要确认这一点,您可以尝试使用console.log进行以下操作:
console.log(join_ids.length) // if this returns zero-based length then its an array
console.log(typeof(join_ids)) // get the TYPE of the variable
console.log(join_ids[0]) // see if you can address the FIRST value individually
// for each loop over the array and console out EACH item in the array
for (var i = 0; i < join_ids.lenght; i++) {
console.log(join_ids[i]);
}
如果你发现它是一个字符串,我会删除撇号
yourstring = yourstring.replace(/'/g, "") // replaces all apostrophes
和逗号后的空格(如果存在),然后将值用双引号括起来并执行
join_ids = join_ids.split(','); // makes the comma separated values an array