-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'}]
6
  • 5
    "I really tried hard" please may you add a minimal reproducible example of your efforts? Commented May 2, 2021 at 22:13
  • 1
    Possible duplicate of stackoverflow.com/questions/5306680/…
    – Kinglish
    Commented May 2, 2021 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
    Commented May 2, 2021 at 22:24
  • @ggorlen could you please write simple code in codepen ?
    – Brad
    Commented May 2, 2021 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 that has 39 answers. Have you tried the code in this existing resource?
    – ggorlen
    Commented May 2, 2021 at 22:57

1 Answer 1

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);
6
  • 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 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
    Commented May 2, 2021 at 22:55
  • @ggorlen it not duplicate question, I want to move object not single element
    – Brad
    Commented May 2, 2021 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
    Commented May 2, 2021 at 22:58
  • I already did but not able to resolve this question? If you here for help please help me
    – Brad
    Commented May 2, 2021 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
    Commented May 2, 2021 at 23:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.