2

I have an array of typed objects - all objects are of type: TodoModel. When I filter them, to adjust the content on the UI, you have to create a new array and adjust the properties on the item that was changed for the filter to refilter in angular2. When I create the new array and add the changed object, the object is of no type, while the other 2 objects in the array are still of type: TodoModel. I can't figure out how to assign a type to the new object :( Thanks in advance for any help!

Here is the code for the logic & below I have a screen shot of the before and after for the mutated arrays:

case 'TOGGLE_TODO':

        const todo: ITodoModel = payload;

        const i = state.indexOf(todo);

        const status = todo.status == "started" ? "completed" : "started";

        const toggledTodo:ITodoModel = Object.assign(<ITodoModel>{}, todo, {status});

        state = [
            //... = spread operator - bascially loops through the array
            ...state.slice(0,i), // check items before
            toggledTodo, // clicked item
            ...state.slice(i + 1) // check items after
        ];

        console.log('toggle state');
        console.log(state);

return state;

Picture of a Before and after array mutation

1 Answer 1

2

If you have access to TodoModel constructor then you can go with something like this:

const toggledTodo:ITodoModel = Object.assign(new TodoModel(), todo, {status})
Sign up to request clarification or add additional context in comments.

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.