8000 Pick returns different object structure from original object with numeric keys · Issue #5948 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content

Pick returns different object structure from original object with numeric keys #5948

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

Open
chreds opened this issue Dec 9, 2024 · 0 comments

Comments

@chreds
Copy link
chreds commented Dec 9, 2024

pick() is returning a different object structure from original object when using numeric keys

Using lodash-es: 4.17.21

Code to reproduce:

import { set, pick } from 'lodash-es'

const index = ["a", "0", "c"]

const orig1 = {
    "a" : {
        "0": {
            "c": "ABC",
            "d": "DEF"
        }
    }
}

const orig2 = {
    "a" : [{
            "c": "ABC"
        },{
            "d": "DEF"
        }]
}

console.log(JSON.stringify(orig1))
console.log(JSON.stringify(orig2))
const calc1 = {...orig1}
const calc2 = {...orig2}
set(calc1, index, "XYZ")
set(calc2, index, "XYZ")
console.log(JSON.stringify(calc1))
console.log(JSON.stringify(calc2))
const pick1 = pick(calc1, [index])
const pick2 = pick(calc2, [index])

console.log(JSON.stringify(pick1))
console.log(JSON.stringify(pick2))

Output:

{"a":{"0":{"c":"ABC","d":"DEF"}}}
{"a":[{"c":"ABC"},{"d":"DEF"}]}
{"a":{"0":{"c":"XYZ","d":"DEF"}}}
{"a":[{"c":"XYZ"},{"d":"DEF"}]}
{"a":[{"c":"XYZ"}]}
{"a":[{"c":"XYZ"}]}

I would have expected pick1 output to be in object format instead of array for the key "0" since that's the structure of the input object.

I know numeric keys are dumb. It just looks like a lot of effort went into set() and get() to support both so thought pick would respect original object format too.

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

No branches or pull requests

1 participant
0