0

I have two input variable with same name, below no. of div can be more

<div>
  <input type="text" name="one[]">
  <input type="text" name="two[]">
</div>
 <div>
  <input type="text" name="one[]">
  <input type="text" name="two[]">
</div>

I am fetching above two textbox in two variable in JavaScript, below is code;

 var one =  $('input[name="one[]"]').map(function(){return $(this).val();}).get().join(',');

 var two =  $('input[name="two[]"]').map(function(){return $(this).val();}).get().join(',');

I want below result, because first div input box variable should be send together.

var three = $a,1$b,2

Anyone can let me know..???

3
  • 1
    does that sequence has any logic ? Commented Oct 3, 2017 at 4:36
  • 1
    var three = $a,1$b,2$c,3 what is that meant to be? Is it a string? An array of some sort? Commented Oct 3, 2017 at 4:36
  • None of variables is an array, so your title doesn't match description, please update your description to properly explain the problem. Commented Oct 3, 2017 at 4:40

3 Answers 3

1

Here is a breakdown that handles if either array is longer than the other:

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)

Here is the clean 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))
Sign up to request clarification or add additional context in comments.

Comments

1

I have jQuery and pure JS versions as below:

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, function(v, i) { return [v, twoArray[i]]; });

var three_pureJS = oneArray.reduce((arr, v, i) => arr.concat(v, twoArray[i]), []);

console.log(three_jQuery.join(''));
console.log(three_pureJS.join(''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

to achieve the exact result of your problem I would do as follows:

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"
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.