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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:55:31+00:00 2026-05-30T17:55:31+00:00

How can I convert something like initialArray array of JSON objects into finalObject map?

  • 0

How can I convert something like initialArray array of JSON objects into finalObject map?

var initialArray = [
             { id:'id1', name:'name1' },
             { id:'id2', name:'name2' },
             { id:'id3', name:'name3' },
             { id:'id4', name:'name4' }
          ];

var finalObject = {
                  'id1':'name1',
                  'id2':'name2',
                  'id3':'name3',
                  'id4':'name4'
               }

Things to consider:

  • IDs are strings.
  • I tried for in loop – couldn’t make it to work – http://jsfiddle.net/5af9R/23/

Any ideas?

  • 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-30T17:55:32+00:00Added an answer on May 30, 2026 at 5:55 pm

    You need to operate on the objects in your array, not strings containing their indexes in the array.

    You should also use a regular for loop to iterate over an array.

    Your JSFiddle, fixed:

    var x = [ {id:'1', img:'img1'}, {id:'2', img:'img2'}, {id:'3', img:'img3'} ];
    var resp = {};
    
    for( var i = 0 ; i < x.length ; i++ ){
        var obj = x[i];
        resp[obj.id] = obj.img;
    }
    
    document.write( JSON.stringify(resp, undefined, 2) );
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert this: 26.72773551940918 Into something like this: 22°12'42N The trick here
How can I convert a HTML template into a live php-editable template? I'd like
How can I convert a number like 150000 to something like 150K? Is it
How can i convert a date of birth like 11/20/1978 to something like November
How can I convert PDF files to HTML with Python? I was thinking something
I have a class Something that implements ISomething . How can I convert/cast from
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
How I can convert this code to LINQ? var TcData = new List<double>(); for(int
How i can convert this array to an UNLIMITED range array? Dim multiArray(2, 2)
Seeing as you can convert any document to a byte array and save it

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.