1

In my Script.js I am trying to push the object to array=>array of objects.

$("#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);
});

My Array will be like this assciated array

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

Anybody know how can I push every object to a array or associated array?

*** filter_saved this an object it will create every time base on options in form(customer_type_selected(choicefield),list_tag_selected(checkboxs))

1
  • Sorry for unclear quiz :What I want is array_obj_filter_saved.push(filter_saved);//push every objects to array so my array_obj_filter_saved = [object_saved1,object_saved2,...] Thx. Commented Dec 24, 2009 at 11:24

3 Answers 3

4

The line below is not an array, it is an object. You can only have integer indexes in arrays, and you use [] to define an array (like you did in your example).

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

You cannot push objects onto an object, instead you set them, using the specific index:

array_obj_filter_saved["keyName"] = "value";
Sign up to request clarification or add additional context in comments.

Comments

3

This is very unclear to me. Is array_obj_filter_saved an array or not?

If you want to append all the elements of an Array to another Array, you can use the concat method:

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

Comments

1

Just like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.