1

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"
    },
]
1
  • what is the output if id 654 have value ABC, while 543 have value DEF? Commented Dec 23, 2020 at 5:18

5 Answers 5

2

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)

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

1 Comment

This does not work if ABC objects are all over the place and not in the end.
1

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

4 Comments

@vickram, I added another solution, which is a bit simpler
He didn't want to sort. He just wanted to swap two items. This can be done in lesser complexity. Your solutions have at least O(n) in time complexity.
Yes, the first solution is O(n) time and O(n) space and the second one is O(n logn) time and O(1) complexity, so the second one is slower, but requires no additional memory.
Hey @vickram, please accept my solution if it was helpful :)
1

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"));

Comments

0

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. 
}

Comments

0

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);

2 Comments

This does not work if the value that we want in the beginning is not 'ABC' but "XYZ" for example
I have updated for any value present in array ABC,XYZ,...

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.