Vickram
Vickram

Reputation: 197

How to shift position index in array of object in JavaScript?

I need some help to shift the position index in the array of objects. I am having a bunch of arrays of objects which need to be shifted at index 0 or 1 based on value key.

const arrayObj1 = [
    {
        id: 234,
        value: "FGH"
    },
    {
        id: 454,
        value: "XYZ"
    },
    {
        id: 654,
        value: "ABC"
    },
    {
        id: 543,
        value: "ABC"
    },
]

Let say I have above array of objects, and I want to shift position to index 0, 1 if the value is "ABC". As of now they are positioning at index 2 and 3. So the array of object will be looks like below after shifting their position.

expectedArrayofObj = [
    {
        id: 654,
        value: "ABC"
    },
    {
        id: 543,
        value: "ABC"
    },
    {
        id: 234,
        value: "FGH"
    },
    {
        id: 454,
        value: "XYZ"
    },
]

Upvotes: 1

Views: 2507

Answers (5)

Himanshu
Himanshu

Reputation: 33

const arrayObj1 = [{
    id: 234,
    value: 'FGH',
  },
  {
    id: 454,
    value: 'XYZ',
  },
  {
    id: 654,
    value: 'ABC',
  },
  {
    id: 543,
    value: 'ABC',
  },
];

let changeValue = 'ABC';

const temp = arrayObj1.filter((item) => item.value === changeValue);
arrayObj1.forEach((item) => {
  if (item.value !== changeValue) temp.push(item);
});
console.log(temp);

const arrayObj1 = [
  {
    id: 234,
    value: 'FGH',
  },
  {
    id: 454,
    value: 'XYZ',
  },
  {
    id: 654,
    value: 'ABC',
  },
  {
    id: 543,
    value: 'ABC',
  },
];

arrayObj1.sort(function (a, b) {
  if (a.value < b.value) return -1;
  else return 1;
  return 0;
});
console.log(arrayObj1);

Upvotes: 0

lissettdm
lissettdm

Reputation: 13080

You can use Array.prototype.reduce()

const arrayObj1 = [{id: 234, value: "FGH"},{id: 454,value: "XYZ"},{id: 654,value: "ABC"},{id: 543,value: "ABC"}];

function shiftBy(value) {
  return arrayObj1.reduce((arr, c) => ((c.value===value && [c, ...arr]) || [...arr, c]),[]);
}

console.log(shiftBy("ABC"));

Upvotes: 1

makinyelure
makinyelure

Reputation: 185

You can do a swap using the index. Good thing is that we are able to do this in O(1) space complexity and O(n) time complexity based on the number of items in the list. The indexOf kind of increased it.

const SHIFT_BY_VALUE = (val1, val2) => {
    let originIndex = arrayObj1.indexOf(arrayObj1.find(e => e.value == val1));
    let itemToShift = arrayObj1[originIndex];

    let destinationIndex = arrayObj1.indexOf(arrayObj1.find(e => e.value == val2));
    arrayObj1[originIndex] = arrayObj1[destinationIndex];
    arrayObj1[destinationIndex] = itemToShift;
    return arrayObj1; // No need to return, Array is by reference. 
}

Upvotes: 0

IvanD
IvanD

Reputation: 2923

const arrayObj1 = [
  {
    id: 234,
    value: 'FGH',
  },
  {
    id: 454,
    value: 'XYZ',
  },
  {
    id: 654,
    value: 'ABC',
  },
  {
    id: 543,
    value: 'ABC',
  },
]

const shift = (arr, value) => {
  const newArr = []

  // Pushing ABC first
  for (const obj of arr) {
    if (obj.value === value) {
      newArr.push(obj)
    }
  }

  // Pushing the rest
  for (const obj of arr) {
    if (obj.value !== value) {
      newArr.push(obj)
    }
  }

  return newArr
}

console.log(shift(arrayObj1, 'ABC'))

If you don't care about the order of other elements, you can use (unstable) sort:

const arrayObj1 = [
  {
    id: 234,
    value: 'FGH',
  },
  {
    id: 454,
    value: 'XYZ',
  },
  {
    id: 654,
    value: 'ABC',
  },
  {
    id: 543,
    value: 'ABC',
  },
]

const shift = (arr, value) => {
  const sortingFunction = (a, _) => (a.value === value ? -1 : +1)
  arrayObj1.sort(sortingFunction)
  return arrayObj1
}

console.log(shift(arrayObj1, 'ABC'))

Upvotes: 1

Aliasgher Nooruddin
Aliasgher Nooruddin

Reputation: 563

const arrayObj1 = [
  {
    id: 234,
    value: 'FGH',
  },
  {
    id: 454,
    value: 'XYZ',
  },
  {
    id: 654,
    value: 'ABC',
  },
  {
    id: 543,
    value: 'ABC',
  },
]
 
index = arrayObj1 .findIndex(resp => resp.value=='ABC')

arrayObj1 .push(...arrayObj1 .splice(0, index));

console.log(arrayObj1)

Upvotes: 2

Related Questions