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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:31:57+00:00 2026-06-14T22:31:57+00:00

I need to convert a JavaScript object of one type: object1: [{a:value1, b:value2, c:value3},

  • 0

I need to convert a JavaScript object of one type:

object1: [{"a":"value1", "b":"value2", "c":"value3"}, {"d":"value4","e":"value5","f":"value6"}] 

To another type of object:

object2 : {"value1":["value1", "value2", "value3"], "value4":["value4","value5","value6"]}

I tried to convert it using this function:

function toObject(arr) {
   var rv = {};
   for (var i = 0; i < arr.length; ++i) {
      rv[i] = arr[i];
   }
   return rv;
}

but I’m getting numerical indexes ([0], [1]) instead of “value1” and “value4”. Could you please give me some hint how can I do the conversion from object1 to object2. Thanks.

  • 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-14T22:31:58+00:00Added an answer on June 14, 2026 at 10:31 pm

    what you want is to concatenate the vectors inmates?

    Try:

    function toObject(arr) {
       var rv = {}, k;
       for (var i = 0; i < arr.length; ++i) {
         for(k in arr[i]){
           rv[k] = arr[i][k];
         }
       }
       return rv;
    }
    

    If this is not what you are looking for then try this:
    [Fixed (with the help of user @user1689607)]

    [edit]:

    Object.keys does not work in older browsers. [Fixed]

    function toObject(arr,_sort) {
        //param1 = Object, param2 = (true:sort, false:default)
        var rv = {}, k, firstV = null, keys, obj, tmp, j,
           ObjK = Object.keys ? function(ke){
                return Object.keys(ke);
           } : function(ke){
                var r = [];
                for(var o in ke){
                    r[r.length] = o;
                }
                return r;
           };
    
        for (var i = 0; i < arr.length; ++i) {
            obj = arr[i];
            tmp = [];
            keys = _sort===true ? ObjK(obj).sort() : ObjK(obj);
            tmp = [obj[keys[0]]];
            for (j = 0; j < keys.length; ++j) {
                tmp[tmp.length] = obj[keys[j]];
            }
            rv[obj[keys[0]]] = tmp;
            firstV = null;
        }
        return rv;
    }
    
    //sort
    console.log(
        toObject([{"a":"value1", "b":"value2", "c":"value3"}, {"d":"value4","e":"value5","f":"value6"}]),
    true);
    
    //default
    console.log(
        toObject([{"a":"value1", "b":"value2", "c":"value3"}, {"d":"value4","e":"value5","f":"value6"}])
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a dataURL to a File object in Javascript in order
I need to convert keys from type string to hash. The name of all
I need to convert a byte[] of size 4 to type long for my
I am using JavaScript Date object to convert milliseconds into a readable minutes:seconds formatted
I want to directly convert a javascript object to string. I used following code.
I need to pass a HashMap object from backing bean (JSF) to JavaScript and
I need to convert a string to a JSON object that gets returned from
I'm working on a programming lanuage, i need to convert an object (like var
I get an error of Cannot convert object of type System.String' to type 'System.Collections.Generic.List'1[...+ContactHolder]'
I have the following array of objects which I need to convert to one

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.