Click here to Skip to main content
15,913,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two array of values like

array1=["I","II","III"];
array2=[1,2,3,4,5,6,7,8,9]

I want to combine these two array using jquery and show the result

combinedarray=["I",1,2,3,4,5,6,7,8,"II",9,"III"];

What I have tried:

I tried array concat.But it cannot show result which I want
Posted
Updated 12-May-16 1:47am
Comments
Karthik_Mahalingam 12-May-16 6:05am    
but what is the condition for adding in that pattern ?

Use Improve question to add more info to the question.
What is the logic of 1-9 before "II"?
Karthik_Mahalingam 12-May-16 6:17am    
1-8 :)
Dil0500 12-May-16 6:19am    
I created two array from json object data with condition "recommend==true or false".I want to show array1 value to 1st,9th,17th.. of gridlayout and show array2 in remaining portion

1 solution

try this,
modify the position value as per your need..

C#
var array1 = ["I", "II", "III"];
      var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
      var arrayout = [];
      var position = [1, 10, 17];

      var i1 = 0, j1 = 0;

      for (var i = 0; i < array1.length + array2.length  ; i++) {

          if (position.indexOf(i + 1) != -1) {
              arrayout.push(array1[i1])
              i1++;
          }
          else {

              arrayout.push(array2[j1]);
              j1++;
          }
      }
 
Share this answer
 
Comments
Dil0500 12-May-16 9:49am    
Thanks for your response.How it work for jsonobject
Karthik_Mahalingam 12-May-16 9:53am    
same only, instead you have to use the property name of each item..
Patrice T 12-May-16 14:54pm    
[1, 10, 17] are positions in result array or positions based on array2 ?
Karthik_Mahalingam 12-May-16 22:11pm    
For array1
Patrice T 12-May-16 23:45pm    
In this case, I fear your code is wrong.
wrong reply !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900