将对象数组转换为字符串数组

时间:2016-12-05 10:41:46

标签: javascript ruby-on-rails

我正在开发一个应用程序,我需要将一个字符串数组传递给后端服务,例如

const ids = []; 
for (let i = 0; i < pIds.length; i++) { 
  ids.push(pIds[i].id);
} 
// pIds is an array of objects [{id: 2, name: 'dd'}]


this.webClient = new Frisbee({
  baseURI: `${Secrets.url_host}/api/v1`,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
});

getData({ continuation = 1, q = '', ids = '' }) {
 return this.webClient.get('/whatever', {
  body: {
    'platform_ids': ids,
  },
}).then(getContent);

}

运行此代码后,如果得到一个ids [2,3]数组,例如

但是当我将它传递给后端(ruby on rails)时,它会像那样到达

[{"0"=>"1", "1"=>"2"}]

如何将其作为[“1”,“2”]?我尝试了很多解决方案,但没有任何效果。

1 个答案:

答案 0 :(得分:0)

我无法解决它,所以我改变后端接受字符串&#34;,&#34;并将其解析为后端的数组。

例如

const ids = {id: 3, id: 4};
const idsForBackend = ids.map(id => id.id).join();
相关问题