如何获取数组中对象的所有值

时间:2016-02-21 21:02:25

标签: javascript arrays json object meteor

在我的meteor应用程序中,此代码

[
    {
        "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
    } ]

给我这个结果

    "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

如何只获取没有括号的值;类似的东西

     {#each format}} 
        <tr>

             {{> postItem}}


        </tr>
        {{/each}} 


<template name="postItem">

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

</template>





Template.hello.helpers({


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

  });

我花了两天没有解决方案:(

感谢您的帮助

编辑:我想要做的是检索模板中{{#each}}中使用的值或游标数组

{{1}}

2 个答案:

答案 0 :(得分:1)

这样可行。

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);

将在下面给出

"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

答案 1 :(得分:0)

&#13;
&#13;
// 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));
&#13;
<pre></pre>
&#13;
&#13;
&#13;