Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

How to apply splice with seamless-immutable? #251

@etairi

Description

@etairi

I'm using seamless-immutable inside React app, and I have a code segment like this in my saga:

let parsedData = yield select(state => state.post.posts);
parsedData = postUtils.deleteLike(parsedData, payload);

And my deleteLike method looks like this:

const deleteLike = (parsedPosts, likeId) => {
    let posts = Immutable.asMutable(parsedPosts);

    Object.entries(posts).forEach(
        ([_, userPosts]) => {
            Object.entries(userPosts).forEach(
                ([_, post]) => {
                    const found = post.likes.filter(l => l.id === likeId);
                    if (found.length > 0) {
                        const index = post.likes.indexOf(found[0]);
                        post.likes.splice(index, 1);
                    }
                }
            );
        }
    );

    return posts;
};

So, basically I want to remove one element from one of the inner arrays that is found inside the object. Though, the above code throws an error saying that splice cannot be invoked on an Immutable data structure. I though the call to asMutable() is going to convert it to a mutable object and store in posts variable. So basically asMutable() doesn't make the nested object mutable all the way? Because I also need at later stage to push into post.likes, not just remove an element as above. So, I want to make the whole object mutable so that I can work comfortably.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions