Javascript 拆分两个阵列&;在js中将其梳理为一个

Javascript 拆分两个阵列&;在js中将其梳理为一个,javascript,Javascript,我有两个同名的输入变量,下面的div数可以更多 <div> <input type="text" name="one[]"> <input type="text" name="two[]"> </div> <div> <input type="text" name="one[]"> <input type="text" name="two[]"> </div> 我想要下面的结果,因

我有两个同名的输入变量,下面的div数可以更多

<div>
  <input type="text" name="one[]">
  <input type="text" name="two[]">
</div>
 <div>
  <input type="text" name="one[]">
  <input type="text" name="two[]">
</div>
我想要下面的结果,因为第一个div输入框变量应该一起发送

var three = $a,1$b,2

任何人都可以告诉我….?

我有jQuery和纯JS版本,如下所示:

var one=“a、b、c”;
var two=“1,2,3”;
var oneArray=one.split(/,/).map(e=>“$”+e);
var twoArray=two.split(/,/).map(e=>,'+e);
var three_jQuery=$.map(oneArray,函数(v,i){return[v,twoArray[i]];});
var three_pureJS=oneArray.reduce((arr,v,i)=>arr.concat(v,twarray[i]),[]);
log(三个jQuery.join(“”));
log(三个pureJS.join(“”))

我有如下jQuery和纯JS版本:

var one=“a、b、c”;
var two=“1,2,3”;
var oneArray=one.split(/,/).map(e=>“$”+e);
var twoArray=two.split(/,/).map(e=>,'+e);
var three_jQuery=$.map(oneArray,函数(v,i){return[v,twoArray[i]];});
var three_pureJS=oneArray.reduce((arr,v,i)=>arr.concat(v,twarray[i]),[]);
log(三个jQuery.join(“”));
log(三个pureJS.join(“”))

要获得问题的确切结果,我将执行以下操作:

var one = "1,2,3";
var two = "a,b,c";
var newOne = a.split(','); // ["1","2","3"]
var newTwo = b.split(','); // ["a","b","3"]
var three = '';

for(var i = 0; i < newOne.length; i++) {
   three += '$' + newOne[i] + ',' + newTwo[i]; //the result would be "$a,1$b,2$c,3"
} 
var one=“1,2,3”;
var two=“a、b、c”;
var newOne=a.split(',');//["1","2","3"]
var newTwo=b.split(',');//[“a”、“b”、“3”]
var三=“”;
for(var i=0;i
要获得问题的确切结果,我将执行以下操作:

var one = "1,2,3";
var two = "a,b,c";
var newOne = a.split(','); // ["1","2","3"]
var newTwo = b.split(','); // ["a","b","3"]
var three = '';

for(var i = 0; i < newOne.length; i++) {
   three += '$' + newOne[i] + ',' + newTwo[i]; //the result would be "$a,1$b,2$c,3"
} 
var one=“1,2,3”;
var two=“a、b、c”;
var newOne=a.split(',');//["1","2","3"]
var newTwo=b.split(',');//[“a”、“b”、“3”]
var三=“”;
for(var i=0;i
下面是一个细分,用于处理任意一个数组是否比另一个数组长:

const one = 'a,b,c'
const two = '1,2,3,4,5'

// split strings by ',' to create arrays
const oneArray = one.split(',')
const twoArray = two.split(',')

// use reduce to accumulate while iterating
const final = oneArray.reduce((all, item, i) => {
    console.log(`CURRENT ${i}:`, '[one: ' + item + ']', '[two: ' + twoArray[i] + ']')

    // take the first element of the two array and put it right after
    // the first element of the one array
    if (item && twoArray[i]) all.push(item, twoArray[i])

    // if there is no elements left in the two array,
    // add the rest of the one array
    if (item && !twoArray[i]) all.push(item)

    // if we are at the last element in the one array,
    // add the rest of the two array to the end 
    if ((i === oneArray.length - 1) && (twoArray.length > oneArray.length)) {
        // this merges in the end of the two array
        return all.concat(twoArray.slice(i + 1))
    }
    return all
}, [])

// return final result
console.log(final)
这是一个干净的决赛:

const one = 'a,b,c'
const two = '1,2,3,4,5'
const oneArray = one.split(',')
const twoArray = two.split(',')

const alternateMerge = (oneArray, twoArray) => oneArray.reduce((all, item, i) => {
  if (item && twoArray[i]) all.push(item, twoArray[i])
  if (item && !twoArray[i]) all.push(item)
  if ((i === oneArray.length -1) && (twoArray.length > oneArray.length)) {
    return all.concat(twoArray.slice(i+1))
  }
  return all
}, [])

console.log(alternateMerge(oneArray, twoArray))

下面是一个细分,用于处理任意一个数组是否比另一个数组长:

const one = 'a,b,c'
const two = '1,2,3,4,5'

// split strings by ',' to create arrays
const oneArray = one.split(',')
const twoArray = two.split(',')

// use reduce to accumulate while iterating
const final = oneArray.reduce((all, item, i) => {
    console.log(`CURRENT ${i}:`, '[one: ' + item + ']', '[two: ' + twoArray[i] + ']')

    // take the first element of the two array and put it right after
    // the first element of the one array
    if (item && twoArray[i]) all.push(item, twoArray[i])

    // if there is no elements left in the two array,
    // add the rest of the one array
    if (item && !twoArray[i]) all.push(item)

    // if we are at the last element in the one array,
    // add the rest of the two array to the end 
    if ((i === oneArray.length - 1) && (twoArray.length > oneArray.length)) {
        // this merges in the end of the two array
        return all.concat(twoArray.slice(i + 1))
    }
    return all
}, [])

// return final result
console.log(final)
这是一个干净的决赛:

const one = 'a,b,c'
const two = '1,2,3,4,5'
const oneArray = one.split(',')
const twoArray = two.split(',')

const alternateMerge = (oneArray, twoArray) => oneArray.reduce((all, item, i) => {
  if (item && twoArray[i]) all.push(item, twoArray[i])
  if (item && !twoArray[i]) all.push(item)
  if ((i === oneArray.length -1) && (twoArray.length > oneArray.length)) {
    return all.concat(twoArray.slice(i+1))
  }
  return all
}, [])

console.log(alternateMerge(oneArray, twoArray))

这个序列有逻辑吗?
var三=$a,1$b,2$c,3
这意味着什么?它是一根绳子吗?某种数组?没有一个变量是数组,因此您的标题与描述不匹配,请更新您的描述以正确解释问题。该序列是否有逻辑?
var three=$a,1$b,2$c,3
这是什么意思?它是一根绳子吗?某种数组?没有变量是数组,因此您的标题与描述不匹配,请更新您的描述以正确解释问题。