0

Here is the current manually populated array that I'm trying to replace with a MiSO dataset:

var stream = new Array();

stream [0] = "life";
stream [0] = "time";

I want to replace that with a dataset that will automatically populate it. Here is example code that successfully gets the values that are manually entered in the array. There are just two values in the example but could be hundreds and I want to reuse it for others. This is why I want to eliminate the manual entry.

var ds = new Miso.Dataset({
        importer: Miso.Dataset.Importers.GoogleSpreadsheet,
        parser: Miso.Dataset.Parsers.GoogleSpreadsheet,
        key : "0AkNLBJFrSMj2dDdKZ1FkaGIxYnF3U0pjeThIY2pjN3c",
        worksheet: "1"
    });
ds.fetch({
  success : function() {
       var magazine = ds.toJSON();

var title1 = magazine[0].name;
var title2 = magazine[1].name;
    alert(title1 + " " + title2);

    },
  error : function() {

  }
});

So, I need to create the stream array from the MISO data set so the format matches:

This doesn't work but is the only way I can explain how the values need to match up in the "new" stream array for each value that will be found in the MISO datatset:

var stream = new Array();

stream [0] = magazine[0].name;
stream [1] = magazine[1].name;

I need to stay with the MISO stuff and I cannot rename the stream array.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
user1452893
  • 838
  • 1
  • 11
  • 29
  • You seem to be looking for a `for` loop: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for. I recommend to read the [MDN JavaScript Guide](https://developer.mozilla.org/en-US/docs/JavaScript/Guide) to learn the JavaScript basics. – Felix Kling Jan 25 '13 at 18:45
  • possible duplicate of [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value) – Felix Kling Jan 25 '13 at 18:47
  • `var magazine = ds.toJSON();` should be `var magazine = ds.rows();` btw. – Felix Kling Jan 25 '13 at 18:49
  • @FelixKling I understand the "teach a man to fish" theory but I'm looking for an example not directions for a trip to the library. Changing to ds.rows does what? I can't see the values then. Thanks anyway but not useful. – user1452893 Jan 25 '13 at 19:07
  • The official website seems to have useful tutorials and API documentation, e.g. http://misoproject.com/dataset/tutorials/accessing_data. Why not `toJSON()`? Because it *should* encode the data as JSON and therefore `magazine` would be a string (but you want an array). If that's not the case then the method name is poorly chosen. Anyways if it helps you more: `var stream = []; for(var i = 0; l < magazine.length; i++) { stream.push(magazine[i].name);}`. – Felix Kling Jan 25 '13 at 21:57
  • @FelixKling Thanks, I appreciate the help and apologize for the frustration earlier. I tried that code but apparently I'm putting it in the wrong spot since it throws "Uncaught ReferenceError: magazine is not defined" Oh, well. I thought this was going to be easy. – user1452893 Jan 25 '13 at 22:15
  • It has to go in the `success` callback. – Felix Kling Jan 25 '13 at 22:18
  • It is? ds.fetch({ success : function() { var magazine = ds.toJSON(); That throws "stream not defined" var stream = []; for(var i = 0; 1 < magazine.length; i++) { stream.push(magazine[i].name);} – user1452893 Jan 25 '13 at 22:24
  • First you wrote `Uncaught ReferenceError: magazine is not defined` and now `stream` is not defined? As you can see there is `var stream = ...` and `var magazine = ...` in the code, so both variables are defined for sure. Note that it is a lower case `L` in `l < magazine.length`, not a `1` (one). I cannot really debug your code remotely, you have to figure this out on your own ;) – Felix Kling Jan 25 '13 at 22:29
  • Yes, the first time I placed it outside of the success callback. The second time within. So, two different errors. I understand you can't debug the code remotely. But, you could modify the example above for a big Danke. ;) – user1452893 Jan 25 '13 at 22:31
  • I wrote an answer, but there is no new information. Keep in mind that you can only access the `stream` array instead the `success` callback. – Felix Kling Jan 25 '13 at 22:40
  • Got it. Will keep at it. – user1452893 Jan 25 '13 at 22:43

1 Answers1

0

As already said, have a look at Access / process (nested) objects, arrays or JSON for a proper explanation of how to access objects and arrays. Some of the Miso tutorials should help too.

This is how you could do it:

ds.fetch({
    success : function() {
       var magazine = ds.toJSON();

       var stream = []; // if not already defined
       for(var i = 0, l = magazine.length; i < l; i++) { 
           stream.push(magazine[i].name);
       }

       processData(stream);
    }
});


function processData(stream) {
   // all the code that needs to access stream goes here
}
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • You say "do whatever you want with stream". It's used in another module so is throwing undefined. The code that the manually created array (shown in original question) fed is this: list = $.map(stream, function (item) { return { service: 'tumblr', user: item, limit: 1, template: { posted: HTML1 + item + HTML2 + item + HTML3 + item + HTML4 } }; }); So, I suspect this approach will continue to fail. – user1452893 Jan 25 '13 at 23:23
  • As I said, I don't know the context of the code, where `stream` is actually defined, when the other code is executed, etc. I can only answer according to the information you provide in your question. It could very well be that it is impossible to combine this code with the other one (though less likely). Maybe you have to make yourself more familiar with variable scope in JS, I don't know. There really is nothing more I could tell you. – Felix Kling Jan 25 '13 at 23:33
  • Thanks for the help. As stated in the original question, the task is to use the MISO dataset to replace the manually created array. Maybe we'll get lucky and someone else can solve it. – user1452893 Jan 25 '13 at 23:41
  • The data is now loaded from the server. That implies you have to wait until the data is received, that's why you use a callback function (the `success` function) to process the data. All the code that needs to work with the data has to be inside or called from the callback. I'm sure you can modify your code so that all the code that needs to work with the `stream` array is inside a function and you just call that function from the `success` callback, passing the `stream` array as argument. If you are not very familiar with JS functions and variables, you really should read a tutorial first. – Felix Kling Jan 25 '13 at 23:55
  • Your code did not work even in the example answer. So, I wasted a lot of time and got a number of lectures about "reading tutorials" and such. This is unfortunate since my prior experience here has always been pleasant. I'm sure it wasn't intentional, but next time if you're not certain about something, you may want to refrain from risking taking folks down the wrong path. – user1452893 Jan 26 '13 at 05:44
  • Ha... it does not work because I had a small error in my code (which you could have detected as well). I wrote `l < i` instead of `i < l`. It works perfectly fine: http://jsfiddle.net/fkling/8MjZW/. It's not my job to make your application work, I have to assume that you are able to integrate this solution with your existing code. I'm certain that the code is correct (there might be different ways to achieve the same thing, but this is one way). I'm just not certain how it will work together with your existing code, because I don't know anything about it. – Felix Kling Jan 26 '13 at 10:36