0

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"
        }
    ]
2
  • array keys are indexes, are you saying you want to move the item at index 0 to index 217? Commented Feb 3, 2022 at 20:07
  • 2
    If you want to use the skipperid as the key, you'd need to copy everything into an Object. Commented Feb 3, 2022 at 20:08

3 Answers 3

2

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'])

Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

I don’t think you are correct. Keys are just strings. Having a key of 0 and another key of 217 does not create an object of 218 keys where 216 are undefined
it doesn't create an object, it creates an array. For example, if you run array=[]; array[217]="Something"; console.log(array); you will get an array of 218 indexes where 216 are undefined and index 217 being 'Something'
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.