在js或jquery中将对象推送到数组?

时间:2009-12-24 11:03:47

标签: jquery

在我的Script.js我试图将对象推送到array =>对象数组。

$("#save_filter").click(function(){
    var filter_saved = new Object();//Create new object to store the filter have been saved.
    var customer_type_selected = $('#id_customer_type :selected').text();
    var list_tag_selected = $("#tag_checked").val();
    var filter_name = $("#put_filter_name").val();

    // Save filter name to the object={tag:[2,3,], customer_type:'TDO', more...}
    filter_saved = {
        customer_type: customer_type_selected,
        tag: list_tag_selected
    };
    var array_obj_filter_saved = []

    //I am trying...
    array_obj_filter_saved.push(filter_saved);//push every objects to array
    alert(array_obj_filter_saved.length);
});

我的数组将像这个相关的数组

array_obj_filter_saved = {"time1":filter_saved1,"time2":filter_saved2}

任何人都知道如何将每个对象都推送到数组或相关数组?

*** filter_saved这是一个它将在每次基于表格中的选项创建的对象(customer_type_selected(choicefield),list_tag_selected(checkboxs))

3 个答案:

答案 0 :(得分:3)

这对我来说非常不清楚。 array_obj_filter_saved是否为数组?

如果您想将Array的所有元素附加到另一个Array,可以使用concat方法:

var a1 = ["a", "b", "c"], a2 = ["d", "e"];
var a3 = a1.concat(a2); // a3 is ["a", "b", "c", "d", "e"]

答案 1 :(得分:3)

下面的行不是数组,而是一个对象。您只能在数组中使用整数索引,并使用[]来定义数组(就像您在示例中所做的那样)。

array_obj_filter_saved = {"time1":filter_saved1,"time2":filter_saved2}

您不能将对象推送到对象上,而是使用特定索引设置它们:

array_obj_filter_saved["keyName"] = "value";

答案 2 :(得分:1)

就像这样:

var x= { a: 1};
x.b = 2;