JavaScript逗号分隔连接数组中的一组元素,而不使用jQuery

JavaScript逗号分隔连接数组中的一组元素,而不使用jQuery,javascript,vue.js,Javascript,Vue.js,我不确定jQuery是否是一个因素,但因为我使用的是Vue和Quasar,所以我想把它放在前面,不使用它 我知道,如果我想将数组转换为逗号分隔的字符串,我会使用 Array.join(',') 这会给我一个很好的逗号分隔字符串。但是,有没有一种方法不进行循环,只连接一组元素 我需要获取一个发布ID数组,删除重复项,然后只合并200个。我的列表可能长达10000条,但我发送数据的web服务一次只能处理200条 30310060,30166592,29704517,29662190,

我不确定jQuery是否是一个因素,但因为我使用的是Vue和Quasar,所以我想把它放在前面,不使用它

我知道,如果我想将数组转换为逗号分隔的字符串,我会使用

  Array.join(',') 
这会给我一个很好的逗号分隔字符串。但是,有没有一种方法不进行循环,只连接一组元素

我需要获取一个发布ID数组,删除重复项,然后只合并200个。我的列表可能长达10000条,但我发送数据的web服务一次只能处理200条

    30310060,30166592,29704517,29662190,29533787,28114741,27456065,27208808,26208975 
然后回去再拿200块


我需要将字符串转换为JSON以将其发送到web服务器。谢谢你的帮助

像这样的东西行吗

joinFirstN = function(array,n,separator){
   sample = array.slice(0,n);
   return(sample.join(separator);)
}
这会将前n个元素分割成一个数组,然后将其合并成一个字符串。 所以你可以这样做:

joinFirstN(array,200,",");

像这样的东西行吗

joinFirstN = function(array,n,separator){
   sample = array.slice(0,n);
   return(sample.join(separator);)
}
这会将前n个元素分割成一个数组,然后将其合并成一个字符串。 所以你可以这样做:

joinFirstN(array,200,",");

如果要避免使用ES6以外的任何东西(无库),可以使用对象来消除数组中的重复值。然后,您可以使用异步函数递归地一次获取任意数量的元素,将它们提交给服务器,并在收到响应后提交下一批。如果您的服务器可以处理多个批,则可以扩展相同的模式以一次提交多个批,而不是串行地提交每个批

//Basically build an object out of the ids. This will dedupe them all.
//Finally, only take the keys (or values, whichever)
const uniqueIds = Object.keys(ids.reduce((ids, id) => {
  ids[id] = id
  return ids
}, {}))

const submitInBatches = async (ids, batchSize) => {
  //If you have no ids, you're done.
  if (ids.length === 0) return

  //Get the next batch of ids, and submit them
  batch = ids.slice(0, batchSize)
  const results = await fetch(`/your/service?${batch.join(',')}`)
  //Do something with the results

  //Then take all of the remaining ids and recursively call them
  return submitInBatches(ids.slice(batchSize), batchSize)
}

submitInBatches(uniqueIds, 200)

如果要避免使用ES6以外的任何东西(无库),可以使用对象来消除数组中的重复值。然后,您可以使用异步函数递归地一次获取任意数量的元素,将它们提交给服务器,并在收到响应后提交下一批。如果您的服务器可以处理多个批,则可以扩展相同的模式以一次提交多个批,而不是串行地提交每个批

//Basically build an object out of the ids. This will dedupe them all.
//Finally, only take the keys (or values, whichever)
const uniqueIds = Object.keys(ids.reduce((ids, id) => {
  ids[id] = id
  return ids
}, {}))

const submitInBatches = async (ids, batchSize) => {
  //If you have no ids, you're done.
  if (ids.length === 0) return

  //Get the next batch of ids, and submit them
  batch = ids.slice(0, batchSize)
  const results = await fetch(`/your/service?${batch.join(',')}`)
  //Do something with the results

  //Then take all of the remaining ids and recursively call them
  return submitInBatches(ids.slice(batchSize), batchSize)
}

submitInBatches(uniqueIds, 200)
Array.from(新集合(ids))
重复数据消除的另一种方法。
Array.from(新集合(ids))
重复数据消除的另一种方法。