-2

I have an array of objects, I want to move any object In the same array. I really tried hard but did not a solution. could someone please help me with how to resolve this issue?

[{name:'user 1'}, {name:'user 2'},{name:'user 3'},{name:'user 4'},{name:'user 5'}]

I want to select {name:'user 2'} and move to after {name:user 3}

The expected result will be

[{name:'user 1'},{name:'user 3'},{name:'user 2'},{name:'user 4'},{name:'user 5'}]
Brad
  • 1
  • 5
    _"I really tried hard"_ please may you add a [mcve] of your efforts? – evolutionxbox May 02 '21 at 22:13
  • 1
    Possible duplicate of https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another – Kinglish May 02 '21 at 22:13
  • Welcome to SO! I'm not sure what you're asking -- do you know where the elements are in advance or do you need to iterate to find them? Swapping elements is as simple as `[a[1], a[2]] = [a[2], a[1]];` and finding them can be done with `array.findIndex(e => e === target)` or `indexOf`. – ggorlen May 02 '21 at 22:24
  • @ggorlen could you please write simple code in codepen ? – Brad May 02 '21 at 22:31
  • @Brad I'm asking for clarification and it hasn't been provided. If I had an answer, I'd add it here instead of codepen. We have a [dupe suggestion](https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another) that has 39 answers. Have you tried the code in this existing resource? – ggorlen May 02 '21 at 22:57
  • Does this answer your question? [Move an array element from one array position to another](https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another) – Rodentman87 May 03 '21 at 03:04

1 Answers1

0

Try this:

// Simultaneously remove element 1 from array, while also setting placeholder 
// variable. [0] because splice produces a new array of numbers. But we only 
// want one value.

const placeholder = array.splice(1,1)[0];

// Reintroduce placeholder variable at a different position, position 2.

array.splice(2, 0, placeholder);
EthanKim8683
  • 46
  • 1
  • 5
  • Welcome to SO and thanks for the answer. However, the typical approach is to simply indicate that this is a duplicate question. We already have a [thread](https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another) with 39 answers and 521k views, so let's just keep it there (your code is virtually identical to multiple answers in that thread). Thanks. – ggorlen May 02 '21 at 22:55
  • @ggorlen it not duplicate question, I want to move object not single element – Brad May 02 '21 at 22:57
  • What's the difference? Your objects _are_ elements. If it's not a duplicate, please edit your question to respond to the requests for clarification, show clearly why it's not a duplicate, and show us your effort at solving it (ask a concrete question about attempted code, not a task). – ggorlen May 02 '21 at 22:58
  • I already did but not able to resolve this question? If you here for help please help me – Brad May 02 '21 at 22:59
  • @ggorlen I want to reposition object an array. For example, I want to move the index 2 objects at the end of the array. could you please try – Brad May 02 '21 at 23:03
  • Chill guys. Brad, objects, elements, they're all the same in the array. Just look on that thread ggorlen linked, any of them should work I'm sure. – EthanKim8683 May 02 '21 at 23:09