put value into a object in order

var dataArray = [];

function setUpData (){
name.val();
address.val();
age.val();
phone.val();

var dataObj = {name, address, age, phone};

dataObj[name] = name.val();
dataObj[address] = address.val();
dataObj[age] = age.val();
dataObj[phone] = phone.val();

dataArray.push(dataObj);

}

i wanted to store my data to obj and then append into array , so i am append want the object to look like this name : jason , age : 27 , something like that, but i am having error doing this.

how i gonna to achieve that ?

Answers 1

  • I have made below code for you this will Print the Address value in your array.you have to initial the Array as below.

        $(document).ready(function(){
        $("p").click(function(){
    
            var dataArray = [];
    
    
            var dataObj = {};
    
            dataObj.tname ="Tst";
            dataObj.address= "Add";
            dataObj.age = "10"
            dataObj.phone = "911"
            dataArray.push(dataObj);
    
           console.log(dataArray.length);
            $.each( dataArray, function(index) {
                 console.log( dataArray[index]["address"]);
            });
    
        });
    });
    

    enter image description here


Related Articles