0

I tried to search for a required solution and help but really couldn't find something to write. I need this JavaScript function to do the following.

I have the following set of lines.

AAAAAAA
BBBBBBB
CCCCCCC
DDDDDDD

And I need to convert the above data into columns so the output would be like this.

ABCD
ABCD
ABCD
ABCD
ABCD
ABCD
ABCD

That means the rows will be converted into columns.

Anyone can help with a JavaScript or jQuery function to get the results?

3
  • 2
    What format is this in? Are they 4 separate arrays? Are they printed out into 4 different rows of an html table? Commented Jun 16, 2016 at 5:52
  • is this array or string? Commented Jun 16, 2016 at 5:52
  • this is a test question actually for an assignment given. See the actual function question here. pastebin.com/kEGBzJzV - see #1. Text Blocking section. Commented Jun 16, 2016 at 5:53

4 Answers 4

3

You can do something like this using Array#forEach method

var data = `AAAAAAA
BBBBBBB
CCCCCCC
DDDDDDD`;

var res = [];

data.split('\n').forEach(function(v) {
  v.split('').forEach(function(v1, i) {
    res[i] = (res[i] || '') + v1;
  })
});

console.log(res.join('\n'));


If both input and output are in array format then you can avoid the String#split and Array#join methods

var data = [
  'AAAAAAA',
  'BBBBBBB',
  'CCCCCCC',
  'DDDDDDD'
]

var res = [];

data.forEach(function(v) {
  v.split('').forEach(function(v1, i) {
    res[i] = (res[i] || '') + v1;
  })
});

console.log(res);

Sign up to request clarification or add additional context in comments.

5 Comments

Per the requirement, your data var is a string. It needs to be an array.
this works, but when I print this in browser, it is not preserving the line breaks. and displaying all the values horizontally. any idea?
thanks. but where do I place join('<br/>') ? I placed it after the function brackets and it broke.
Thank you so much. that works. one more question. how do I make this whole thing as a function so I can supply different arguments to it instead of fixed AAAABBBB and so forth?
@RockingNinja : put them inside an array and pass array as argument.... then generate and return the result array...
0

There may be better solutions than this, Try this:

var data = ["AAAAAAA",
            "BBBBBBB",
            "CCCCCCC",
            "DDDDDDD"];

var output = [];
data.map(function(a){
    for(var i=0; i<a.length;i++){
          if(!output[i])
              output[i] = "";
         output[i] += a[i];
    }
});
console.log(output);

Comments

0

Per the requirement in the question, this will do it:

var arr = ["AAA", "BBB", "CCC"];
var result = ['', '', ''];
for (var i = 0; i < arr.length; i++) {
    var str = arr[i].split('');
    for (var j = 0; j < str.length; j++) {
        result[j] += str[j];
    }
}
    console.log(result);

Comments

0

var data = ["AAAAAAA",
  "BBBBBBB",
  "CCCCCCC",
  "DDDDDDD"
];

var mapedData = data[0].split('').map(function(string, i) {
  return data.map(function(string1, i1) {
    return string1.charAt(i);
  }).join('');
});

console.log('required data ---', mapedData);

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.