1

I have an array with two objects, I want to be able to swap the key values of the two objects..

    "people":[
        {
            "name":"bob",
            "lastname": "johnson",
            "slot_id": 1
        },
        {
            "name":"terry",
            "lastname": "lucas",
            "slot_id": 2
        },
    ]

I would like to be able to swap the two slot_id's like so..

    "people":[
        {
            "name":"bob",
            "lastname": "johnson",
            **"slot_id": 2**
        },
        {
            "name":"terry",
            "lastname": "lucas",
            **"slot_id": 1**
        },
    ]

thanks in advance!

9
  • 2
    And what have you tried until now? Can you share with us, then we can help to fix the issue you have on achieving this goal? Commented Jan 15, 2020 at 16:41
  • If this was ever to grow, what would it look like - ie if there were three items, how would you swap around? Commented Jan 15, 2020 at 16:44
  • So that's the thing im having a hard time wrapping my head around where to start.. I know I have to iterate through, get the slot_id of each then somehow swap them.. It will never grow this is basically being used to update the slot of the people then will go back to being an empty array.. sorry if that's confusing clearly im pretty confused myself lol.. Commented Jan 15, 2020 at 16:50
  • If you have a hard time where to start on this, you should read another JS tutorial. Preferably one, that isn't aimed at "get started quick", but takes its time to explain properly. The previous one(s) obviously did a bad job. Commented Jan 15, 2020 at 16:53
  • 1
    @DylanJannetty You have some answers, I've posted one, don't hesitate to ask questions if there is something you don't understand. Commented Jan 15, 2020 at 17:11

2 Answers 2

2

If you want to swap 2 values, you'll need a temporary variable so you won't lose a value while you assign a new one:

const tmp = people[0].slot_id

people[0].slot_id = people[1].slot_id
people[1].slot_id = tmp

There is also another possibility if you don't want to use a temporary variable:

people[0].slot_id += people[1].slot_id
people[1].slot_id = people[0].slot_id - people[1].slot_id
people[0].slot_id -= people[1].slot_id

You can also use array destructuring (proposed by @ASDFGerte)

[people[0].slot_id, people[1].slot_id] = [people[1].slot_id, people[0].slot_id]
Sign up to request clarification or add additional context in comments.

1 Comment

You can do [people[0].slot_id, people[1].slot_id] = [people[1].slot_id, people[0].slot_id]; if you want to avoid an explicit temporary variable.
1
const person1Id = people[0].slot_id

people[0].slot_id = people[1].slot_id
people[1].slot_id = person1Id

4 Comments

The answer is same as above. Just you have changed the variable .
@Forhad did you mean to write this comment on the other post? If you look at the timestamps I posted this answer before the other answer was submitted...
I m really sorry. Yeah your answer is correct. Thanks
@Forhad It was the first answer, I've posted another answer because it was missing explanations and I've added more details and other way to swap variables.

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.