-1

In my meteor app , this code

       var format = [];
       var formats = [];



        var length = result.formats.length - 1;
        for (var i = 0 ; i <= length; i++) {
          var type = result.formats[i].type;
          var type2 = type.split(/[;,]/);

              var typee = type2[0];
               var resolutions = result.formats[i].resolution;
               var urls = result.formats[i].url;


                format.push({
                  "Type": typee,
                  "resolution": resolutions
                  // "url": urls
                })


        };



        var formatt = JSON.stringify(format, null, 4);
        Session.set('format', JSON.stringify(format, null, 4));
        console.log(formatt);

give me this result

[
    {
        "Type": "video/webm",
        "resolution": "360p"
    },
    {
        "Type": "video/mp4",
        "resolution": "360p"
    },
    {
        "Type": "video/x-flv",
        "resolution": "240p"
    },
    {
        "Type": "video/3gpp",
        "resolution": "240p"
    },
    {
        "Type": "video/3gpp",
        "resolution": "144p"
    },
    {
        "Type": "video/mp4",
        "resolution": "360p"
    },
    {
        "Type": "video/webm",
        "resolution": "360p"
    },
    {
        "Type": "video/mp4",
        "resolution": "240p"
    },
    {
        "Type": "video/webm",
        "resolution": "240p"
    },
    {
        "Type": "video/mp4",
        "resolution": "144p"
    },
    {
        "Type": "video/webm",
        "resolution": "144p"
    },
    {
        "Type": "audio/webm",
        "resolution": null
    },
    {
        "Type": "audio/mp4",
        "resolution": null
    },
    {
        "Type": "audio/webm",
        "resolution": null
    },
    {
        "Type": "audio/webm",
        "resolution": null
    },
    {
        "Type": "audio/webm",
        "resolution": null
    } ]

How can I do to get only the values without parentheses; something like that

    "Type": "video/webm",
    "resolution": "360p"


    "Type": "video/mp4",
    "resolution": "360p"


    "Type": "video/x-flv",
    "resolution": "240p"


    "Type": "video/3gpp",
    "resolution": "240p"


    "Type": "video/3gpp",
    "resolution": "144p"


    "Type": "video/mp4",
    "resolution": "360p"


    "Type": "video/webm",
    "resolution": "360p"


    "Type": "video/mp4",
    "resolution": "240p"


    "Type": "video/webm",
    "resolution": "240p"


    "Type": "video/mp4",
    "resolution": "144p"


    "Type": "video/webm",
    "resolution": "144p"


    "Type": "audio/webm",
    "resolution": null


    "Type": "audio/mp4",
    "resolution": null


    "Type": "audio/webm",
    "resolution": null


    "Type": "audio/webm",
    "resolution": null


    "Type": "audio/webm",
    "resolution": null

I spent two days without solution :(

Thank's for you help

Edit: what I want to do is to retrieve an array of values or cursors used in {{#each}} in a template

     {#each format}} 
        <tr>

             {{> postItem}}


        </tr>
        {{/each}} 


<template name="postItem">

        {{Type}}
        {{resolution}} 
        {{url}}

</template>





Template.hello.helpers({


     format:function(){
     return Session.get('format');
    }

  });
abdou
  • 53
  • 7
  • 2
    What exactly are you trying to achieve? Why do you need the data in this format (which doesn't follow any standard and doesn't seem to be every usable)? How are you planning to continue processing this data once it is on this format? – Felix Kling Feb 21 '16 at 21:05
  • You just want to access the properties of an object appearing many times in an array? – Robert Moskal Feb 21 '16 at 21:07
  • In other words...what is the higher level objective or problem you are trying to solve? – charlietfl Feb 21 '16 at 21:10
  • @FelixKling @charlietfl; I edit the question to reply your question – abdou Feb 21 '16 at 21:38

2 Answers2

1

This will work.

var arr = [{"Type":"video/webm","resolution":"360p"},{"Type":"video/mp4","resolution":"360p"},{"Type":"video/x-flv","resolution":"240p"},{"Type":"video/3gpp","resolution":"240p"},{"Type":"video/3gpp","resolution":"144p"},{"Type":"video/mp4","resolution":"360p"},{"Type":"video/webm","resolution":"360p"},{"Type":"video/mp4","resolution":"240p"},{"Type":"video/webm","resolution":"240p"},{"Type":"video/mp4","resolution":"144p"},{"Type":"video/webm","resolution":"144p"},{"Type":"audio/webm","resolution":null},{"Type":"audio/mp4","resolution":null},{"Type":"audio/webm","resolution":null},{"Type":"audio/webm","resolution":null},{"Type":"audio/webm","resolution":null}]

var output = arr.map(function(obj){
  return JSON.stringify(obj).replace(/{|}/g,'')
}).join('\r\n');

console.log(output);

will give below

"Type":"video/webm","resolution":"360p"
"Type":"video/mp4","resolution":"360p"
"Type":"video/x-flv","resolution":"240p"
"Type":"video/3gpp","resolution":"240p"
"Type":"video/3gpp","resolution":"144p"
"Type":"video/mp4","resolution":"360p"
"Type":"video/webm","resolution":"360p"
"Type":"video/mp4","resolution":"240p"
"Type":"video/webm","resolution":"240p"
"Type":"video/mp4","resolution":"144p"
"Type":"video/webm","resolution":"144p"
"Type":"audio/webm","resolution":null
"Type":"audio/mp4","resolution":null
"Type":"audio/webm","resolution":null
"Type":"audio/webm","resolution":null
"Type":"audio/webm","resolution":null
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
0

// Just some setup
var formats = [ { "Type": "video/webm", "resolution": "360p" }, { "Type": "video/mp4", "resolution": "360p" }, { "Type": "video/x-flv", "resolution": "240p" }, { "Type": "video/3gpp", "resolution": "240p" }, { "Type": "video/3gpp", "resolution": "144p" }, { "Type": "video/mp4", "resolution": "360p" }, { "Type": "video/webm", "resolution": "360p" }, { "Type": "video/mp4", "resolution": "240p" }, { "Type": "video/webm", "resolution": "240p" }, { "Type": "video/mp4", "resolution": "144p" }, { "Type": "video/webm", "resolution": "144p" }, { "Type": "audio/webm", "resolution": null }, { "Type": "audio/mp4", "resolution": null }, { "Type": "audio/webm", "resolution": null }, { "Type": "audio/webm", "resolution": null }, { "Type": "audio/webm", "resolution": null } ]
var result = {formats:formats};


// This will do the job
var result_string = formats.map(function(f) {
  var x = JSON.stringify(f, null, 4);
  return x.substring(1, x.length-1);
}).join('\n');

// Just display the results
document.getElementsByTagName('pre')[0].appendChild(document.createTextNode(result_string));
<pre></pre>
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97