在 Meteor 中将数组转换为查询字符串 [英] Convert Array to Query string in Meteor

查看:46
本文介绍了在 Meteor 中将数组转换为查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Echonest 获取艺术家资料.我需要在查询字符串中多次使用名为bucket"的参数.我试图用我传递的对象的数组来设置它.这可以在数组中传递吗?

I am trying to get a artist profile from Echonest. I need to have a parameter named 'bucket' multiple times in the query string. I am trying to set it with an array with the object that I am passing. Is this possible to pass this in an array?

我有这个:

bucket:['biographies', 'images', 'artist_location', 'urls'];

我想要这个:

bucket=biographies&bucket=images&bucket=artist_location&bucket=urls

客户:

getArtistProfile = function(artistName){
    var params = {
        format:'json',
        bucket:['biographies', 'images', 'artist_location', 'urls'],
        name:artistName
    };

     Meteor.call('getEchoNestData', params, function(error, result) {
        if (error)
            console.warn(error);
        else
            console.log(result);
    });
};

服务器方法:

getEchoNestData:function(type, params){
    check(type, String);

    params.api_key = Meteor.settings.echonest.apiKey;

    var result = HTTP.get('http://developer.echonest.com/api/v4/artist/profile' + type, {timeout:5000, params:params});
    return result;
 }

推荐答案

我可能误解了您的问题,但您似乎在询问如何动态构建一组查询字符串参数.你可以有一个像这样的简单助手:

I could be misinterpreting your question, but it seems like you are asking how to dynamically build a set of query string parameters. You could have a simple helper like this:

function getParams( arr ) {
    var params = [];

    for ( i = 0; i < arr.length; ++i ) {
        params.push( 'bucket=' + arr[ i ] );
    }

    return params.join( '&' );
}

并像这样传入参数值数组:

and pass in your array of param values like this:

var params = getParams(bucket);

http://jsfiddle.net/7vaLcjxs/

这篇关于在 Meteor 中将数组转换为查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆