如何在循环javascript上获取变量名,变量类型是数组

时间:2017-03-20 06:18:19

标签: javascript

这是我在js中的变种

    var schemapost = {};
    var schemacrumb = {};
    var og = {};
    var meta = {};

    $.get('json', function(resp) {
        schemapost = {
            "@context":"http://schema.org",
            "@type":"Article",
            "mainEntityOfPage":permalink,
            "headline":title,
            "description":description
        };

        schemacrumb = {
            "@context":"http://schema.org",
            "@type":"BreadcrumbList",
        };

        og = {
            "fb:app_id":resp.facebook_app_id,
            "fb:admins":resp.facebook_id,
        };

        meta = {
            "description":description,
            "keywords":post_keyword,
            "robots":"noodp"
        };
    });

所以我收集一个变量中的所有变量

    var schema = [schemapost,schemacrumb,og,meta];

我尝试循环该变量而感到困惑

foreach ( a in schema ){
    jQuery.ajax({
        type: "POST",
        url: "/make_schema.php",
        data: { data : JSON.stringify(???EACH VARIABLE NAME??), 
                post_name : slug , 
                variablename : ???EACH VARIABLE NAME?? },
    });
}

显然很愚蠢,但我试试这个:

var schema = ["schemapost","schemacrumb","og","meta"];

schema.forEach(function(entry) {
    jQuery.ajax({
        type: "POST",
        url: "/kode/post/make_json",
        data: {value : JSON.stringify(entry), post_name:slug, variablename:entry},
    });
});

并且输出是字符串..不是我想要的可变数组数据, 任何建议如何循环数组变量并获取每个变量名称,以便我可以用于数据:ajax函数中的variablename?

1 个答案:

答案 0 :(得分:0)

只需发送您已创建的完整架构数组

var schema = [schemapost,schemacrumb,og,meta];

使用Jquery post方法:

$.ajax({
    type: "POST",
    url: "/make_schema.php",
    data: { data : schema , 
            post_name : slug
});

并在后端进程中捕获完整数组作为数组