Javascript Loop through and Concatenate 2 Arrays

I am trying to execute a loop and combine 2 arrays, for example, below, I do not know how many values ​​there will be, since the user could buy 1 or 100 products, so he needs to complete the loop.

Array1 = ['ABC', 'DEF', 'GHI']

Array2 = ['123', '45', '6789',]

I need to output:

ABC:123|DEF:45|GHI:6789

I currently have the code ...

function() {
  var array_one = ['ABC', 'DEF', 'GHI', 'JKL'];
  var array_two = ['179.99', '349.99', '399.99', '389'];
  for (var i = 0; i < array_one.length; i++) {
    for (var j = 0; j < array_two.length; j++) {
      return(array_one[i] + ":" + array_two[j] + "|");
    }
  }
}

This prints out only one value and does not go in cycles in any ideas where my cycle is interrupted.

+4
source share
7 answers

This outputs only one value and does not go in cycles, any ideas where my cycle is interrupted.

, what is the reason?

, return , .

, ?

, :

 return(array_one[i] + ":" + array_two[j] + "|");

?

Note: , , .

, :

function() {
  var array_one = ['ABC', 'DEF', 'GHI', 'JKL'];
  var array_two = ['179.99', '349.99', '399.99', '389'];
  var result = "";
  for (var i = 0; i < array_one.length; i++) {
     result += array_one[i] + ":" + array_two[i] + "|";
  }
  return result;
}
0

, , :

var array1 = ['ABC', 'DEF', 'GHI']
var array2 = ['123', '45', '6789']

var result = array1.map(function(item, index) {
  return item + ':' + array2[index]
}).join('|')

console.log(result)

ES2015:

var result = array1.map((item, index) => `${item}:${array2[index]}`).join('|')
+3

, . , .

,

var result="";
for (var i = 0; i < array_one.length; i++) {     
     result += (array_one[i] + ":" + array_two[i] + "|");    
}
return result;
+1

map() array_two, join() .

var array_one = ['ABC', 'DEF', 'GHI', 'JKL'];
var array_two = ['179.99', '349.99', '399.99', '389'];

var result = array_one.map(function(e, i) {
  return e + ':' + array_two[i]
}).join('|')

console.log(result)
Run code
+1
source

you can use Array.reduce

var array_one = ['ABC', 'DEF', 'GHI', 'JKL'];
var array_two = ['179.99', '349.99', '399.99', '389'];

var result = array_one.reduce(function(p, c, i, a) {
  p += c + ":" + array_two[i]
  if (i < a.length - 1)
    p+="|"
  return p;
}, "")

console.log(result)
Run code

C for cycle

var array_one = ['ABC', 'DEF', 'GHI', 'JKL'];
var array_two = ['179.99', '349.99', '399.99', '389'];

var result = "";
for(var i = 0; i< array_one.length; i++){
  result += array_one[i] + ":" + array_two[i]
  if (i < array_one.length - 1)
    result += "|"
}
console.log(result)
Run code
+1
source

var arr1 = ['ABC', 'DEF', 'GHI'],
    arr2 = ['123', '45', '6789'],
    result = '';
    
    //iterate over every element from arr1 and add it into the 
    //result string with corresponding value from arr2
    arr1.forEach((v,i) => result += v + ':' + arr2[i] + '|'); 
    console.log(result.slice(0, -1)); //cut the `|` sign from the end of the string
Run code
0
source

Only one 'for loop' is enough for the result you need

     var  returnValue = "";
     for (var i = 0; i < array_one.length; i++)
        {
        returnValue+ =array_one[i] + ":" + array_two[j] + "|";
        }
     return  returnValue;
-1
source

Source: https://habr.com/ru/post/1671037/


All Articles