Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
how to change keys of array

I have the following array of objects. How do I change the keys of the array (0,1) to the skipperid (217,1)

[0:
        {
            "type": "RES",
            "date": "2022-05-14",
            "doy": 133,
            "skipperid": 217,
            "boat": "Laura",
            "start": "09:00:00",
            "end": "22:00:00",
            "spots": 5,
            "fname": "David",
            "lname": "Cross"
        }, 
     1:{
            "type": "SAIL",
            "date": "2022-05-14",
            "doy": 133,
            "skipperid": 1,
            "boat": "Avrora",
            "start": "10:00:00",
            "end": "13:00:00",
            "spots": 3,
            "fname": "Bob",
            "lname": "Smith"
        }
    ]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Javascript arrays always have numeric indexes. If you want to have keys, you'll need to convert into an object as shown here.

let data = [{
    "type": "RES",
    "date": "2022-05-14",
    "doy": 133,
    "skipperid": 217,
    "boat": "Laura",
    "start": "09:00:00",
    "end": "22:00:00",
    "spots": 5,
    "fname": "David",
    "lname": "Cross"
  },
  {
    "type": "SAIL",
    "date": "2022-05-14",
    "doy": 133,
    "skipperid": 1,
    "boat": "Avrora",
    "start": "10:00:00",
    "end": "13:00:00",
    "spots": 3,
    "fname": "Bob",
    "lname": "Smith"
  }
]

data = data.reduce((b,a) => ({...b, [a.skipperid]:a}), {});
console.log(data['217'])

about 4 years ago · Juan Pablo Isaza Report

0

I definitely suggest using an object like @Kinglish example.

Moving the position from current index to index by skipperid creates an array of size (highest skipperid), leaving all index != skipperid as undefined

USE AN OBJECT

This is messy:

let data = [{
    "type": "RES",
    "date": "2022-05-14",
    "doy": 133,
    "skipperid": 217,
    "boat": "Laura",
    "start": "09:00:00",
    "end": "22:00:00",
    "spots": 5,
    "fname": "David",
    "lname": "Cross"
  },
  {
    "type": "SAIL",
    "date": "2022-05-14",
    "doy": 133,
    "skipperid": 1,
    "boat": "Avrora",
    "start": "10:00:00",
    "end": "13:00:00",
    "spots": 3,
    "fname": "Bob",
    "lname": "Smith"
  }
]
data.forEach((o,i,self)=> {
if(o?.skipperid != i){
self[o.skipperid] = o;
self[i] = undefined
}
})
console.log(data)

about 4 years ago · Juan Pablo Isaza Report

0

Observations :

  • JSON you provided is not a valid JSON.
  • result will be an object instead of an array.

Working Demo :

const arr = [{
        "type": "RES",
        "date": "2022-05-14",
        "doy": 133,
        "skipperid": 217,
        "boat": "Laura",
        "start": "09:00:00",
        "end": "22:00:00",
        "spots": 5,
        "fname": "David",
        "lname": "Cross"
    },
    {
        "type": "SAIL",
        "date": "2022-05-14",
        "doy": 133,
        "skipperid": 1,
        "boat": "Avrora",
        "start": "10:00:00",
        "end": "13:00:00",
        "spots": 3,
        "fname": "Bob",
        "lname": "Smith"
    }
];

const obj = {};

arr.forEach((item) => {
    obj[item.skipperid] = item
});

console.log(obj)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!