Mritunjay Upadhyay
Mritunjay Upadhyay

Reputation: 1094

Change position of array element

let cards = [
   { name: 'a', value: '234'},
   { name: 'b', value: '876' },
   { name: 'c', value: '765'},
   { name: 'd', value: '980' }
];
let defaultCard = 'c';

I want to the defaultCard to be the first card in cards array. In this case result will be

cards = [
   { name: 'c', value: '765'},
   { name: 'a', value: '234'},
   { name: 'b', value: '876' },
   { name: 'd', value: '980' }
]

What is the best possible way.

What I have done is to get index of default card and swap with the 0 index.

function getIndex(arr, defaultCard) {
  return arr.findIndex((item) => item.name === defaultCard);
}
function getSortedCard(arr) {
  let index = getIndex(arr, defaultCard);
  if (index < 1) return arr;
  let temp = arr[index];
  arr[index] = arr[0];
  arr[0] = temp;
  return arr;
}

const newArr = getSortedCard(cards);
console.log(newArr);

Upvotes: 1

Views: 180

Answers (3)

Goran Stoyanov
Goran Stoyanov

Reputation: 2311

Here's a way to do it via destructuring and Array.filter method.

let cards = [
   { name: 'a', value: '234'},
   { name: 'b', value: '876' },
   { name: 'c', value: '765'},
   { name: 'd', value: '980' }
];
let defaultCard = 'c';

cards = [cards.find(item => item.name === defaultCard), ...cards.filter(item => item.name !== defaultCard)];

console.log(cards);

Upvotes: 2

Ori Drori
Ori Drori

Reputation: 191936

If you don't want to change the original array, you can find the index (Array.findIndex()), and then construct a new array by putting the element at the index at the top, and spreading the slices that come before and after the index:

const moveToTop = (name, arr) => {
  const idx = arr.findIndex(o => o.name === name);
  
  if(idx === -1) return arr;
  
  return [
    arr[idx],
    ...arr.slice(0, idx),
    ...arr.slice(idx + 1)
  ]
}

const cards = [{"name":"a","value":"234"},{"name":"b","value":"876"},{"name":"c","value":"765"},{"name":"d","value":"980"}];
const defaultCard = 'c';

const result = moveToTop(defaultCard, cards);

console.log(result);

If you want to mutate the array, use Array.splice() to remove the item, and use Array.unshift() to insert it at the top:

const moveToTop = (name, arr) => {
  const idx = arr.findIndex(o => o.name === name);
  
  if(idx === -1) return arr;
  
  arr.unshift(...arr.splice(idx, 1));
  
  return arr;
}

const cards = [{"name":"a","value":"234"},{"name":"b","value":"876"},{"name":"c","value":"765"},{"name":"d","value":"980"}];
const defaultCard = 'c';

const result = moveToTop(defaultCard, cards);

console.log(result);

Upvotes: 1

aprouja1
aprouja1

Reputation: 1810

You can use the Array.sort()method and have the match sort higher:

let cards = [{
    name: 'a',
    value: '234'
  },
  {
    name: 'b',
    value: '876'
  },
  {
    name: 'c',
    value: '765'
  },
  {
    name: 'd',
    value: '980'
  }
];
let defaultCard = 'c';

let ans = cards.sort((a, b) => {
  if (a.name === defaultCard) {
    return -1
  }
  return 1
})

console.log(ans)

Upvotes: 1

Related Questions