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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:12:21+00:00 2026-05-23T11:12:21+00:00

I am struggling with a particular Javascript problem and I hope someone can help.

  • 0

I am struggling with a particular Javascript problem and I hope someone can help.

I have a list of name and value pairs, e.g.:

Apple 50
Banana 60
Grape 70
Apple 40
Orange 30

I am trying to create a Javascript object out of these values:

var myObj = {};

and then for all items in the list:

myObj[name] = value;

but as you can see some names are in the list more than once (e.g. Apple), so in this case I would like to add it to the same property of the object by converting the value in the object to an array and adding the new value to the array.

So myObj would contain:

Apple = [50,40]
Banana = 60
Grape = 70
Orange = 30

Anyone help?

Thanks,

AJ

  • 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-23T11:12:22+00:00Added an answer on May 23, 2026 at 11:12 am

    Like this:

    var obj = { };
    for (var i = 0; i < keys.length; i++) {
        var k = keys[i];
    
        if (k in obj) {
            if (obj[k] instanceof Array) {
                obj[k].push(values[i]);
            else
                obj[k] = [ obj[k], values[i] ];
        } else
            obj[k] = values[i];
    }
    

    If some of the values are already arrays, they will be flattened, not nested.

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

Sidebar

Related Questions

I'm somewhat new to SQL queries, and I'm struggling with this particular problem. Let's
Hi i'm struggling to write a particular MySQL Join Query. I have a table
I'm struggling with a (usually simple to deal with) problem. I have a database
I've been struggling with the following problem. I have a series of function objects,
I'm struggling to model a particular relationship with ActiveRecord. I currently have the following
Struggling with styling the mouse over for a button ... I have managed to
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified
Been struggling with this simple selector problem a couple of hours now and must
been struggling with an issue now for a day or two. I have an
I'm struggling to find the right terminology here, but if you have jQuery object...

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.