0

How may I build a function in javascript that do the following :

Input :

var data = [null, 1, 2, 3, null, null, 2, null, 4]

output:

dataset = [ 
[null, 1, 2, 3, null, null, null, null, null],
[null, null, null, null, null, null, 2, null, null],
[null, null, null, null, null, null, null, null, 4], 
]

in fact in this example, my input have 3 set of no null values : these sets are : [1, 2, 3], [2] and [4]

from these sets I want builds arrays that have these values (of not null sets) and that conserve the same index as the input array


Thank you

11
  • 2
    Can you explain how the input becomes the output? Commented Dec 29, 2015 at 18:24
  • It is based on this question: stackoverflow.com/questions/34513679/… Commented Dec 29, 2015 at 18:25
  • How would you know, @CodeiSir? I believe it would be frowned upon to have multiple accounts as one could be tempted to up-vote one's own question(s). Commented Dec 29, 2015 at 18:27
  • the input to output doesn't make any sense. Why does the first one arbitrarily retain three non-null values when the other two arrays only retain one each? Commented Dec 29, 2015 at 18:27
  • 1
    @PraveenKumar I assumed the same, but it doesn't make sense where the array splits off to a new row. I could understand one non-null value per row, or even splitting it into thirds, but neither of those work. Commented Dec 29, 2015 at 18:30

3 Answers 3

1

Just a proposal with Array.prototype.reduce()

var data = [null, 1, 2, 3, null, null, 2, null, 4],
    result = [];

data.reduce(function (r, a, i) {
    if (a !== null) {
        if (r === null) {
            result.push(Array.apply(null, { length: data.length }).map(function () { return null; }));
        }
        result[result.length - 1][i] = a;
    }
    return a;
}, null);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

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

Comments

1

Here's my result:

var data = [null, 1, 2, 3, null, null, 2, null, 4];
var dataset = [[]];
for(var i = 0; i < data.length; i++) {
  if(!data[i]) {
    if(!data[i-1]) {
      dataset[dataset.length-1].push(data[i]);
    } else {
      for(var fillRight = i; fillRight < data.length; fillRight++) {
        dataset[dataset.length-1].push(null);
      }
      dataset.push([null]);
      for(var fillLeft = 0; fillLeft < i; fillLeft++) {
        dataset[dataset.length-1].push(null);
      }
    }
  } else {
    dataset[dataset.length-1].push(data[i]);
  }
}

Comments

0

Here you are..

var a = [null, 1, 2, 3, null, 2, null, null, 4, null];
var b = []; // result
for (var arr = null, i = 0; i < a.length; i++) {
  if (a[i] === null) {
    arr = null;
  } else {
    if (!arr) {
      if(b.length) b[b.length-1] = b[b.length-1].concat(Array(a.length-i).map(function(){return null}))
      b.push(arr = Array.apply(null, Array(i)).map(function() {
        return null
      }));
    }
    arr.push(a[i]);
  }
}
document.body.textContent = JSON.stringify(b); // just print the result

based on this answer: https://stackoverflow.com/a/34513955/4339170

I only added the lines:

      if(b.length) b[b.length-1] = b[b.length-1].concat(Array(a.length-i).map(function(){return null}))
      b.push(arr = Array.apply(null, Array(i)).map(function() {return null}));

where the first fills up the end with nulls when a section is done, and the second fills up the front of a new section with nulls

3 Comments

Hi CodeiSir, Good thank you, however it does not do the all the job : The result I got is this : [null, 1, 2, 3, undefined × 5] [null, null, null, null, null, 2, undefined × 2] [null, null, null, null, null, null, null, null, 4] I guess the result table should be filled by null before enter the loop ?
Well you could also fill up an array with nulls first and then just place the new items at the correct position. Would be more performant, by I don't have the time right now
@taboubim it gives me [[null,1,2,3,null,null,null,null,null],[null,null,null,null,null,2,null,null],[null,null,null,null,null,null,null,null,4]] as you can see in the output

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.