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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:07:24+00:00 2026-06-04T09:07:24+00:00

Given a JavaScript array of objects, how can I get the key and value

  • 0

Given a JavaScript array of objects, how can I get the key and value of each object?

The code below shows what I’d like to do, but obviously doesn’t work:

var top_brands = [ { 'Adidas' : 100 }, { 'Nike' : 50 }];
var brand_options = $("#top-brands");
$.each(top_brands, function() {
  brand_options.append($("<option />").val(this.key).text(this.key + " "  + this.value));
});

So, how can I get this.key and this.value for each entry in the array?

  • 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-04T09:07:26+00:00Added an answer on June 4, 2026 at 9:07 am

    Change your object.

    var top_brands = [ 
      { key: 'Adidas', value: 100 }, 
      { key: 'Nike', value: 50 }
    ];
    
    var $brand_options = $("#top-brands");
    
    $.each(top_brands, function(brand) {
      $brand_options.append(
        $("<option />").val(brand.key).text(brand.key + " " + brand.value)
      );
    });
    

    As a rule of thumb:

    • An object has data and structure.
    • 'Adidas', 'Nike', 100 and 50 are data.
    • Object keys are structure. Using data as the object key is semantically wrong. Avoid it.

    There are no semantics in {Nike: 50}. What’s “Nike”? What’s 50?

    {key: 'Nike', value: 50} is a little better, since now you can iterate an array of these objects and values are at predictable places. This makes it easy to write code that handles them.

    Better still would be {vendor: 'Nike', itemsSold: 50}, because now values are not only at predictable places, they also have meaningful names. Technically that’s the same thing as above, but now a person would also understand what the values are supposed to mean.

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

Sidebar

Related Questions

Given an instantiated JavaScript object/class can I make an Ajax call from that class
I'd like to have JavaScript objects within another JavaScript object as such: Issues: -
I have a collection of objects in JavaScript like this: Object collection = new
in my javascript, i have an Array of Calendar objects. Each Calendar objects has
Via jquery / Javascript Given a number 1234567891234 How can I concat, or truncast
Given a string like the following in JavaScript var a = 'hello world\n\nbye world\n\nfoo\nbar\n\nfoo\nbaz\n\n';
Given an Youtube video id how can I determine from JavaScript whether the video
I'm basically trying to create a generic object in javascript that I can use
Given an associative array (of the sort returned by jQuery.serializeArray() ) like this: [
Possible Duplicate: Convert flat array [k1,v1,k2,v2] to object {k1:v1,k2:v2} in JavaScript? I want to

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.