-2

I have multiple arrays of indexes, representing an order:

[[0,1,2], [2,0,1], [1,2,0], [0,1,2], ...]

I need to construct a new array of indexes with a length equal to the total number of indexes in the input while ordering the values using the position indicated in each array in the input.

This would be the output of the input above:

[0, 1, 2, 5, 3, 4, 7, 8, 6, 9, 10, 11, ...]
  • The total length is 12, so the list of indexes will contain 0-11
  • The first array of the input is 0, 1, 2 so the output starts with 0, 1, 2
  • The second array of the input is 2, 0, 1 and the next 3 indexes in the new list are 3-5. ordering these using the second array of the input results in 5,3,4
  • And so on...
Joshua
  • 34,237
  • 6
  • 59
  • 120

4 Answers4

1

I'm confused by your example, but it sounds like you want to flatten your array by one level. You can do so like this:

var arrays = [[0,1,2], [2,0,1], [1,2,0], [0,1,2]];
var result = [].concat.apply([], arrays);
console.log(result);

If you're using a library like Underscore, there are built-in methods to do that, like the flatten method.

bejado
  • 1,330
  • 11
  • 19
1

You could use Array#reduce, Array#forEach and the length of the actual array for the count. Then push the sum of all length until now and the value of inner array to the result set.

var array = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 1, 2]],
    result = [];

array.reduce(function (r, a) {
    a.forEach(function (b) {
        result.push(r + b);
    });
    return r + a.length;
}, 0);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • actually it is no dupe. – Nina Scholz Mar 06 '17 at 22:18
  • Thanks, accepted this answer for the less loc. IsNull's answer may also be correct. Will try it later. Also thanks everyone for your effort. Most didn't get it because the result is a new array with new elements coded from input array, and not only array merge.. – Hugo Caires Mar 06 '17 at 23:25
0

The input and output examples are confusing, but I think what you want it this:

var array = [...] // your array
var result = Array.prototype.concat.apply([], array);
Gab
  • 917
  • 9
  • 20
0

I believe that I interpreted your question based on the expected output, and edited the question accordingly.

You'll need to loop over the sub-arrays in the input and get the next set of indexes for the output, then add those to the output using the order of the sub-array.

This seems to do the trick:

var input = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 1, 2]];

var output = [];

// add values to the output for each of the inputs
$.each(input, function (index, value) {
    // get the next and last values to add
    var startIndex = output.length;
    var lastIndex = (startIndex + value.length) - 1;

    // make an array of sequential values from startIndex to lastIndex
    var arrayToAdd = [];
    for (var i = startIndex; i <= lastIndex; i++) {
        arrayToAdd.push(i);
    }

    // add the values from arrayToAdd in the order of the indexes in the input
    $.each(value, function (innerindex, innerindexvalue) {
        output.push(arrayToAdd[innerindexvalue]);
    });

});

console.log(output);
Nathan Hase
  • 638
  • 4
  • 11
  • Thanks IsNull, thanks for the edit also, didn't really know how to explain. I accepted the answer from Nina just because of reduced loc. But i'll try this one later – Hugo Caires Mar 06 '17 at 23:27
  • Thanks! It is more concise code. I went for a verbose approach to explain the steps more clearly. – Nathan Hase Mar 06 '17 at 23:41