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
-
2Can you explain how the input becomes the output?Lee Taylor– Lee Taylor2015年12月29日 18:24:47 +00:00Commented Dec 29, 2015 at 18:24
-
It is based on this question: stackoverflow.com/questions/34513679/…CoderPi– CoderPi2015年12月29日 18:25:03 +00:00Commented 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).Andrue Anderson– Andrue Anderson2015年12月29日 18:27:48 +00:00Commented 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?Joseph Marikle– Joseph Marikle2015年12月29日 18:27:49 +00:00Commented 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.Joseph Marikle– Joseph Marikle2015年12月29日 18:30:46 +00:00Commented Dec 29, 2015 at 18:30
3 Answers 3
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>');
Comments
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
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
[[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