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 6637531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:19:36+00:00 2026-05-25T23:19:36+00:00

I have a JSON object of data containing items and their sales listed by

  • 0

I have a JSON object of data containing items and their sales listed by month. I’d like to list the item sales/month inside a DIV alongside other info about each item.

Here’s my JSON object:

var my_object= [{"item_id":"11","month":"February","sales":"7"}, {"item_id":"4","month":"February","sales":"2"},{"item_id":"11","month":"March","sales":"1"},{"item_id":"4","month":"March","sales":"2"}]

here’s how i’d like the final HTML to look:

<ul><li><button value="4">item_id_4</button><div>where the item_4 sales/month (i.e., 2,2) should go</div></li>
<li><button value="11">item_id_11</button><div>where the item_11 sales/month (i.e, 7,1) should go</div></li></ul>

lastly, here’s my not yet correct jquery/javascript attempt to display the item counts/month.

$(function(){   
$('button').each(function(){

var my_object = [{"item_id":"11","month":"February","sales":"7"},  {"item_id":"4","month":"February","sales":"2"},{"item_id":"11","month":"March","sales":"1"},{"item_id":"4","month":"March","sales":"2"}];

var button_value=$(this).val();

 $.each(my_object, function(i,item){

  if(item.item_id==button_value){       
    $('div').append(item.sales);
        } 

    });
}); 
});

As it stands, this script displays the sales for both items in EACH div, i.e., 2,2,7,1 instead of just displaying 2,2 and 7,1 in their respective item_id_4 and item_id_11 divs separately. This makes it seem like the if statement is doing at least something right in that the “my_object” item sales are listed in the order of the button value and not as they are ordered in the “my_object”.

I’m not sure whether the issue is with the each() functions, the IF statement, or maybe something else not mentioned here. Any help fixing this script would be greatly appreciated!

  • 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-05-25T23:19:37+00:00Added an answer on May 25, 2026 at 11:19 pm

    Your script seems to work fine except for the append line, where you tell jQuery to select all divs on the page and append your sales figures to them. Obviously that’s not quite what you wanted. 🙂

    Using your current HTML structure, changing that line to select the next div seems to work. (I’ve also saved the current button in $this.)

    $this.next("div").append(item.sales);
    

    Code here http://jsfiddle.net/meloncholy/ZhrZU/

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

Sidebar

Related Questions

I have a json object named data like below And I have a function
I have a big json object containing cell data from a sample spreadsheet that
I have a json object like this: [{ thing: Top, data: { childs: [{
I have a JSON object containing a number of strings. I also have a
I have this json object (which i don't really know what is inside) and
I have an json Object like this. {'01/19/2012': Array[1],'02/19/2012': Array[7],'03/19/2012': Array[6]} Now i want
I have a JSON object that looks like this. [ { id : 23,
I have a JSON object items which has few items that I want to
If i have a JSON Object var Obj = { col1 : 'data' };
I have a JSON data object and a text/html javascript template written using the

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.