JavaScript数组在索引处分割

时间:2016-04-04 12:41:01

标签: javascript

我有一个像:

这样的数组
var arr = ['Number 1', 'name', 'Number 2', 'name']

如何从此数组中获取如下所示的字符串:

var str = 'Number 1: name, Number 2: name'

5 个答案:

答案 0 :(得分:5)

为此,您可能需要Array#reduce,请参阅评论:

// The array
var arr = ['Number 1', 'name', 'Number 2', 'name']

// Start with no delimiter
var delim = "";

// Array#reduce calls the callback once for each entry, passing in
// the accumulator as the first argument and the entry as the second
var str = arr.reduce(function(acc, entry) {
    // Add this entry onto the accumulator
    acc += delim + entry;

    // Toggle between ", " and ": " as the delim (the first time we have
    // "", which means we'll switch to ": ")
    delim = delim === ", " ? ": " : ", ";

    // Return the new value of the delim
    return acc;
}, "");
// ^^---- initial value of the accumulator is blank

document.body.innerHTML = "<pre>" + str + "</pre>";

或者,您可以使用reduce的其他变体来执行此操作,其中您没有为累加器提供初始值:

// The array
var arr = ['Number 1', 'name', 'Number 2', 'name']

// Start with the first delimiter we want to use
var delim = ": ";

// Array#reduce calls the callback slightly differently the
// first time: It passes entry 0 as the first argument and entry 1
// as the second. From then on it keeps passing the accumulator.
var str = arr.reduce(function(acc, entry) {
    // Add this entry onto the accumulator
    acc += delim + entry;

    // Toggle between ", " and ": " as the delim (the first time we have
    // "", which means we'll switch to ": ")
    delim = delim === ": " ? ", " : ": ";

    // Return the new value of the delim
    return acc;
});

document.body.innerHTML = "<pre>" + str + "</pre>";

或者实际上,您可以使用mapjoin

// The array
var arr = ['Number 1', 'name', 'Number 2', 'name']

// Append the desired delimiter to each entry except the last,
// then join the resulting array
var str = arr.map(function(entry, index) {
    if (index < arr.length - 1) {
        entry += index % 2 == 1 ? ", " : ": ";
    }
    return entry;
}).join("");

document.body.innerHTML = "<pre>" + str + "</pre>";

答案 1 :(得分:3)

你可以通过,

来做到
var arr = ['Number 1', 'name', 'Number 2', 'name'],res ="";
for(var i=0,len=arr.length;i<len;i+=2){
  res += arr[i] + ":" + arr[i+1] + ((i+2 != len) ? "," : "");
}

将for循环的步骤设置为2.在迭代期间,获取与current indexcurrent index + 1相关的数组值。通过给定:分隔符来连接它。你的工作已经完成。

DEMO

答案 2 :(得分:0)

您可以将public void updateInUi() { // send your request and get list of updated items List<> newItems = getYourUpdatedListHere() yourDataList.addAll(newItems); adapter.notifyDataSetChanged(); } .join()

一起使用

.replace()

答案 3 :(得分:0)

此代码将根据需要为您提供正确的输出 - &gt;

    var str = "";
    var arr = ['Number 1', 'name', 'Number 2', 'name'];
    var i=0;
    while(i<arr.length)
   {

   str += i==0 ? "" : ","
    str+=arr[i];
    i++;
    str+=":";
    str+=arr[i];

   }

答案 4 :(得分:0)

当存在其他解决方案时,您不应该使用字符串连接,只是因为它不太可读且难以维护。

&#13;
&#13;
var arr = ['Number 1', 'name', 'Number 2', 'name'];

var str = Array.from({length: arr.length / 2}).map(() => arr.splice(0, 2).join(' :')).join(', ');

document.body.innerHTML = `<pre>${str}</pre>`;
&#13;
&#13;
&#13;