1

If this were .NET, I'd ask how to convert List<List<MyClass> to List<MyClass>. However, I'm not very good with javascript and don't know how to ask that as a question using Javascript terminology!

My javascript object comes through like

enter image description here

And is created as:

js_datasets.push({
"DataItem0": {
lzabel: "This",
data: [[1408710276000, null],[1408710276000, 15]]
},
"DataItem1": {
lzabel: "That",
data: [[1408710276000, null],[1408710276000, 15]]
},
});


js_datasets.push({
"DataItem22": {
lzabel: "And other",
data: [[1408710276000, null],[1408710276000, 5]]
},
"DataItem23": {
lzabel: "And lastly",
data: [[1408710276000, null],[1408710276000, 1]]
},
});

Each object is the same "type" (if it matters).

I'd like to create a single list but I am failing to do so. My efforts are

var myDataSet = []; //this is the results of what I want, ideally as a single list

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

    if (i==0) {
        myDataSet.push(js_datasets[i]);
    }
    else {
        myDataSet.concat(js_datasets[i]);//does nothing
        myDataSet.join(js_datasets[i]);//does nothing
    }

...more logic

As you can see with the above, I've tried using push, concat and join.

If I update the code to only use push (and never use concat and join) then I get all the values I want, but again, as an array within an array.

Using concat and join do not add to the list.

So, if we can assume the 12 items in the array (pictured) all contain 10 items, I'd like to have a single list of the 120 items!

How can I simply convert this multidimension array (is it multidimension) to a single dimension array.

Dave
  • 8,163
  • 11
  • 67
  • 103
  • 1
    `concat` doesn't modify anything, but returns a new array! – Bergi Aug 26 '14 at 10:31
  • 1
    @Bergi from the image in the question, the items of the _Array_ don't look like they're actually _Arrays_ (which means the answers in the flatten question don't actually work here) – Paul S. Aug 26 '14 at 10:41
  • 1
    @PaulS. You might be correct (Dave, please clarify by posting your input as JSON and I'll reopen), but that would mean that `concat` etc wouldn't work anyway and we'd need some kind of custom iteration mechanism. – Bergi Aug 26 '14 at 10:46

1 Answers1

3

This will be a bit complicated, as the items in your Array js_datasets are not Arrays, but a more generic Object. This means you can't assume the keys will be in order if you try to read them

Lets write some helper functions to account for this;

function dataItemCollectionToArray(o) {
    var keys = Object.keys(o);
    // assuming no non-DataItem keys, so next line commented out
    // keys = keys.filter(function (e) {return e.indexOf("DataItem") === 0;});
    keys.sort(function (a, b) { // ensure you will get the desired order
        return +a.slice(8) - +b.slice(8);
    });
    return keys.map(function (e) {return o[e];});
}

Now you can loop over js_datasets performing this conversion

var myDataSet = [], i;
for (i = 0; i < js_datasets.length; ++i) {
    // assuming no gaps, if you need to add gaps, also find min, max indices
    // in `dataItemCollectionToArray`, and check them in each iteration here
    myDataSet.push.apply(myDataSet, dataItemCollectionToArray(js_datasets[i]));
}

Please note that Object.keys and Array.prototype.map may require polifills if you wish to support old browsers, i.e. IE<=8


An easier solution however, may be to re-write how js_datasets is constructed so that the Objects you are pushing are more Array-like or indeed pushing true Arrays, perhaps with a couple extra properties so you know the offset for the first index. This would mean you can use flatten methods that you'll find around the internet

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • I wouldn't sort the keys at all, by definition they're unordered so they might as well stay that way - just `extend` one object with all keys. *When* you sort them, you will need to use a custom compare function to avoid `DataItem100` be sorted before `DataItem99` – Bergi Aug 26 '14 at 11:01
  • @Bergi Thanks, I did not consider the sort problem. – Paul S. Aug 26 '14 at 11:03