-2

My javascript code like this :

<script type="text/javascript">
    var clubs = [ 
        {id: 3, name : 'chelsea'},
        {id: 6, name : 'city'},
        {id: 7, name : 'liverpool'},
        {id: 10, name : 'manchester united'},
        {id: 16, name : 'arsenal'}
    ];
    console.log(clubs)
</script>

I want to change the key of array to value of id

For example

key of {id: 7, name : 'liverpool'}, is 2

I want to change 2 to be 7

Another example

key of {id: 16, name : 'arsenal'} is 4

It to be 16

How can I do it?

Update :

From the clubs array

index of {id: 3, name : 'chelsea'} is 0

index of {id: 6, name : 'city'} is 1

index of {id: 7, name : 'liverpool'} is 2

index of {id: 10, name : 'manchester united'} is 3

index of {id: 16, name : 'arsenal'} is 4

I want to change it to be like this :

index of {id: 3, name : 'chelsea'} is 3

index of {id: 6, name : 'city'} is 6

index of {id: 7, name : 'liverpool'} is 7

index of {id: 10, name : 'manchester united'} is 10

index of {id: 16, name : 'arsenal'} is 16

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
moses toh
  • 12,344
  • 71
  • 243
  • 443
  • 1
    Not clear with the requirement. Can you please rephrase the problem – Nikhil Aggarwal Apr 11 '18 at 09:35
  • 1
    No. Well, yes, but you shouldn't. This would result in a wrong length of the array. – Jeff Apr 11 '18 at 09:37
  • 3
    Would be better to use an object instead but your [XY problem](http://xyproblem.info) is most likely to [find an object by id from the array](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects). – JJJ Apr 11 '18 at 09:37
  • Please share your attempt. – gurvinder372 Apr 11 '18 at 09:38
  • The only way you can do that is by reordering the array. Since if you change the location from one element. The keys of the other ones will be changed as well. You might be better off just rebuiding the array all together. – Jens Ingels Apr 11 '18 at 09:38
  • @nikhilagw I update my question – moses toh Apr 11 '18 at 09:39
  • 1
    f.e. if you do `clubs[16] = {id: 16, name: 'arsenal'};` - then you have what you 'want', but `clubs.length` would then be 17... with only 5 objects in it. – Jeff Apr 11 '18 at 09:39
  • @SuccessMan - And what about the missing id's? – Nikhil Aggarwal Apr 11 '18 at 09:40

3 Answers3

3

Use reduce and create an object, instead of array, having id as key.

var clubs = [ 
    {id: 3, name : 'chelsea'},
    {id: 6, name : 'city'},
    {id: 7, name : 'liverpool'},
    {id: 10, name : 'manchester united'},
    {id: 16, name : 'arsenal'}
];
var resData = clubs.reduce((a,x)=>{
 a[x.id] = x;
 return a;
},{})
console.log(resData)
Durga
  • 15,263
  • 2
  • 28
  • 52
  • 2
    This is the answer, either manually create the object or use a reducing function such as the example given here. The other answer of creating a sparse array would solve this but is bound to cause problems if you ever want to do anything other than look ups. – Liam MacDonald Apr 11 '18 at 09:41
  • @Durga The length to be `3`. Should only `5` – moses toh Apr 11 '18 at 09:44
1

You could take id as index and create a sparse array.

var clubs = [{ id: 3, name : 'chelsea' }, { id: 6, name : 'city' }, { id: 7, name : 'liverpool' }, { id: 10, name : 'manchester united' }, { id: 16, name : 'arsenal' }];
    
clubs = clubs.reduce((r, o) => (r[o.id] = o, r), []);
    
console.log(clubs);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Use reduce like Durga, but pass an object with a getter as length property.

var clubs = [ 
    {id: 3, name : 'chelsea'},
    {id: 6, name : 'city'},
    {id: 7, name : 'liverpool'},
    {id: 10, name : 'manchester united'},
    {id: 16, name : 'arsenal'}
];
var resData = {
    get length(){
        return Object.getOwnPropertyNames(this).filter(function(i){
            return /^\d+$/.test(i)
        }).length
    }
};
clubs.reduce(function(a,x){
    a[x.id] = x;
    return a;
},resData);
Kevin Drost
  • 383
  • 4
  • 7