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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:12:46+00:00 2026-06-13T11:12:46+00:00

I have this javascript array: [[‘a’, ‘x’, 1], [‘a’, ‘y’, 2], [‘b’, ‘x’, 3],

  • 0

I have this javascript array:

[['a', 'x', 1],
 ['a', 'y', 2],
 ['b', 'x', 3],
 ['b', 'z', 4],
 ['c', 'y', 5],
 ['c', 'z', 6]]

How do I pivot it to something like below with the 2nd column (‘x’, ‘y’, ‘z’) from above going across.

[['a', 1, 2, null],
 ['b', 3, null, 4],
 ['c', null, 5, 6]]

EDIT:
Sorry I was unclear. The answers so far seem to be referencing a static length/value for x, y, z. The array will be dynamic and can have anything in the 2nd column (ex. ‘t’,’u’,’v’,’w’ instead of ‘x’,’y’,’z’). I think I need to fill the array up first with nulls for all the possible combinations and then push in the values.

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-13T11:12:47+00:00Added an answer on June 13, 2026 at 11:12 am

    Going by Fabricio’s comment, here is how you can accomplish something similar:

    var result = {};
    for(var i=0;i< basearray.length;i++){
        if(!result[basearray[i][0]]){
            result[basearray[i][0]]={};
        }
        result[basearray[i][0]][basearray[i][1]]=basearray[i][2];
    }
    

    Note that this returns an object or hashmap, not strictly an array, but the data is more organised and it can easily be turned into an array if you so wish. Here is a demonstration (check your console).

    By adding this code:

    var count=0;
    for(var key in result){
        result[count]=[];
        result[count][0]=key;
        result[count][1]=result[key].x||null;
        result[count][2]=result[key].y||null;
        result[count][3]=result[key].z||null;
        count++;
    }
    

    your result object now simulates both structures, your original array of arrays, and the suggested key value pairs. You can see the results here: http://jsfiddle.net/9Lakw/3/

    Here is what result looks like:

    {
       "a":{
          "x":1,
          "y":2
       },
       "b":{
          "x":3,
          "z":4
       },
       "c":{
          "y":5,
          "z":6
       },
       "0":[
          "a",
          1,
          2,
          null
       ],
       "1":[
          "b",
          3,
          null,
          4
       ],
       "2":[
          "c",
          null,
          5,
          6
       ]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have a javascript array written like this... var json = [ {id:1, title:Test
I have Javascript array which contains User object. I have created this array from
I have a javascript array of strings and I want to display below this
1) I have this Javascript array: lang=new Array(); lang[sq]=Albanian; lang[ar]=Arabic; lang[en]=English; lang[ro]=Romanian; lang[ru]=Russian; 2)
I have this array, that I want to parse into javascript. $offerText[] = (
I have this Javascript code I inherited from another developer. I am very new
I have this javascript code below that uses jquery, it is suppoed to be
I have this JavaScript string that contains and array of malformed JSON and I
I have this JavaScript function to create a table with image cells: function Draw(array)

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.