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
Open
@chreds

Description

@chreds

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0