0

I am trying to move an object from one array to another. Think of it like adding / moving a friend from non-friend to friend. I have two arrays, which can be seen below, and I am trying to move an object (i.e. a friend) from possible to current via it's 'id'. In the below example, I am trying to move Parker from possible to current with id = 2.

state = {
    current: [
        {
            id: 1,
            name: 'peter'
        }
    ],
    possible: [
        {
            id: 2,
            name: 'parker'
        }
    ]
}


function addFriend(state, action) {
  const { current, possible } = state;
  const addedFriend = Object.assign(
    {},
    state.possible.splice(action.payload.index, 1)
  );

  current.push(addedFriend);

  const newState = { current, possible };
  return newState;
}
0

2 Answers 2

1

Since you can remove multiple elements with splice(), it returns an array. Index the result to get the specific object. You don't need to use Object.assign(), that just copies the value (which just converts the array into an object whose properties are the array indexes).

var state = {
    current: [
        {
            id: 1,
            name: 'peter'
        }
    ],
    possible: [
        {
            id: 2,
            name: 'parker'
        }
    ]
};

function addFriend(state, action) {
  const { current, possible } = state;
  const addedFriend = state.possible.splice(action.payload.index, 1)[0];

  current.push(addedFriend);

  const newState = { current, possible };
  return newState;
}

state = addFriend(state, {payload: { index: 0 }});
console.log(state);

I'm not sure why you're returning a new state object, since you're modifying the old state in place.

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

6 Comments

Currently current is resulting in: { current: [ { id:1, name: 'peter' }, { 0: { id:2, name: 'parker' } } ] } - i somehow need that 0 to disappear. 1) The reason I am returning new state is because I am using redux, and a don't want to duplicate state. 2) I'm using Object.assign() to try an combat the problem above...
I've updated the answer to show how to do it without Object.assign.
@Barmar This works but I think you are mutating the state when you are creating addedFriend variable.
current.push() mutates the state, I took that from your original code.
how would you do it via id (not via index?)
|
0

It is not that time-efficient if you want a fast running code. But it follows immutability.

We just ignore the item from possible, and add it to current.

state = {
    current: [
        {
            id: 1,
            name: 'peter'
        }
    ],
    possible: [
        {
            id: 2,
            name: 'parker'
        }
    ]
}


function addFriend(state, action) {
  const { current, possible } = state;
  return {
    ...state,
    current: current.concat(possible[action.payload.index]),
    possible: possible.filter((_, index) => index !== action.payload.index)
  }
}

state = addFriend(state, {payload: {index: 0}})
console.log(state)

Comments

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.