Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order changed by updateIn #48

Open
jeff303 opened this issue Apr 13, 2021 · 0 comments
Open

Order changed by updateIn #48

jeff303 opened this issue Apr 13, 2021 · 0 comments

Comments

@jeff303
Copy link

jeff303 commented Apr 13, 2021

Consider the following snippet, run against Icepick 2.4.0:

var icepick = require("icepick")

const BEFORE = {
  first: {
    name: "First",
    items: [{key: "first-a", value: 12}]
  },
  second: {
    name: "Second",
    items: [{key: "second-a", value: 13}]
  },
  third: {
    name: "Third",
    items: [{key: "third-a", value: 15}]
  }
};

console.log(BEFORE);

const AFTER = icepick.updateIn(BEFORE, ["second", "items"], items => [
  ...items,
  {
    key: "second-x",
    value: 42
  },
]);

console.log(AFTER);

In this case, the output is:

Object
first: Object {name: "First", items: [Object {key: "first-a", value: 12}]}
second: Object {name: "Second", items: [Object {key: "second-a", value: 13}]}
third: Object {name: "Third", items: [Object {key: "third-a", value: 15}]}

Object
third: Object {name: "Third", items: [Object {key: "third-a", value: 15}]}
second: Object {items: [Object {key: "second-a", value: 13}, Object {key: "second-x", value: 42}], name: "Second"}
first: Object {name: "First", items: [Object {key: "first-a", value: 12}]}

Notice that the order of keys has been reversed by this operation (where an item was added to the items collection of the 2nd value in the map). I realize that maps are inherently unordered, but it might still be useful if this operation didn't change the order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant