想把输出变成数组nodejs

Want to turn output into an array nodejs

我有一个返回数组的函数,如下所示:

.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(message)的输出;

    { 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 的数组。

I.E.

join_ids[0] = '26c14292-a181-48bd-8344-73fa9caf65e7', join_ids[1] = '64405c09-61d2-43ed-8b15-a99f92dff6e9'

谢谢!

根据您的第二个代码片段,它看起来已经是一个数组。要确认这一点,您可以尝试 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

和逗号后的 space 如果存在,则只需将值用双引号引起来并执行

join_ids = join_ids.split(',');  // makes the comma separated values an array

Fiddle example here.