3

this is my code can un help with it how to write slice function properly

 export default function(state={inputs:[ ]}, action) {
        console.log(action.index);
        switch (action.type) {
        case APPEND_INPUT:
            return { ...state, inputs: state.inputs.concat({value:"",key:"1",type:'input'})};
        case APPEND_TEXTAREA:
            return { ...state, inputs: state.inputs.concat({value:"",key:"",type:'textarea'})};
        case APPEND_EMAIL:
            return { ...state, inputs: state.inputs.concat({value:"",key:"",type:'email'})};
        case REMOVE_INPUT:
            return  {inputs: state.inputs.slice(0)};

        default:
            return state;
        }
    }

2 Answers 2

4

Or:

{inputs: state.inputs.filter((_, i) => i !== action.index))
Sign up to request clarification or add additional context in comments.

Comments

3

Slice the array up to the index, and concat it to the slice from the index + 1 onwards:

{ 
    inputs: state.inputs.slice(0, action.index).concat(state.inputs.slice(action.index + 1))
}

If you use ES6 you can do this instead of concat:

{ 
    inputs: [...state.inputs.slice(0, action.index), ...state.inputs.slice(action.index + 1)]
}

1 Comment

it isn't working onclick its going to default state can u please help me with it

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.