Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:49:14+00:00 2026-06-18T09:49:14+00:00

How can I push an object into a jquery data() object that is an

  • 0

How can I push an object into a jquery data() object that is an array. I want to end up with the data property numbers containing an array of objects, that I acquire by looping through some html of a particular class. I don’t understand how I can push each object into the array.

My first question is, if I have some data in an object, how can I look at the whole object. It seems like it used to be that I could do

$('#div1').data('values', {'one' : 'UNO', 'two' : 'DUE'});
console.log($('#div1').data('values'))

and Chrome would give me a little expandable object to look at. Now I just see [object Object] I can still see them if I do

console.log($('#div1').data('values').one).

But that seems a little more inconvenient if I don’t know exactly what’s in the data() object. It would be useful for checking to see how close I am to achieving this.

Once I assign all my data to their respective objects,

$(document).ready(function(){
  $('#div1').data('values', {'one' : 'UNO', 'two' : 'DUE'});
  $('#div2').data('values', {'three' : 'TRE', 'four' : 'QUATTRO'});
  $('#div3').data('values', {'five' : 'CINQUE', 'six' : 'SEI'});
  $('#div4').data('values', {'seven' : 'SETTE', 'eight' : 'OTTO'});
}); 

how can I loop through these objects (all with a shared class add) and put the objects they contain in data.values into another data() object? Here I’m trying to do it on the body‘s data object, numbers:

  `$('body').data('numbers', []);`

so that

$('body').data('numbers') =

['div1': {
    'one': 'UNO',
    'two': 'DUE'
},
'div2': {
    'three': 'TRE',
    'four': 'QUATTRO'
},
'div3': {
    'five': 'CINQUE',
    'six': 'SEI'
},
'div4': {
    'seven': 'SETTE',
    'eight': 'OTTO'
}]

my attempt has failed:

    $('.add').each(function (index, element) {
        $('body').data(numbers, {
            element.attr('id'): element.data('values')
        //could not understand how to use push here, though it seems likely that's the answer
     });

jsbin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:49:15+00:00Added an answer on June 18, 2026 at 9:49 am

    JavaScript does not have associative arrays so you have to use an Object. Then use bracket notation for the property (key) names:

    var values = {};
    $('.add').each(function (index, element) {
        values[element.id] = $(element).data('values');
    });
    $('body').data('numbers', values);
    

    jsBin

    About your [object Object], you may be accidentally doing string concatenation with the object before outputting it to the console, else it is a bug in your Chrome console.

    Using an array you can use .push to push items onto the end of the array:

    var values = [];
    $('.add').each(function (index, element) {
        values.push( $(element).data('values') );
    });
    

    Bin

    After having the array initialized and stored inside the element’s .data(), you can later push items into it by simply grabbing a reference to the Array object and calling .push() on it:

    var values = [];
    $('.add').each(function (index, element) {
        values.push( $(element).data('values') );
    });
    $('body').data('numbers', values);
    
    $('body').data('numbers').push({'nueve': 'nine'});
    //logs "nueve" too as Array/Objects/Functions are passed by reference and the
    //previous command pushed an item into the Array object referenced ("stored")
    //in the $('body').data('numbers')
    console.log( $('body').data('numbers') );
    

    Bin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to draw an array of balls, I can push
I would like to convert an array of jQuery objects into a single jQuery
Fiddle here I have an array of objects that I want to map directly
I am trying to create an array in my JavaScript object that contains objects
I want to save Json into two variables so that I can manipulate one,
I'm trying to create an array from selected table rows so I can push
Can the .NET RIA Service automatically push data to the silverlight client without the
Can anyone confirm that after changing the Apple Push Certificate to follow the new
How can i create a chain of viewcontrollers and push them into navigation item
How can I know that my iPhone is connected to the Apple Push Notification

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.