Swap key with value JSON

后端 未结 18 2486
花落未央
花落未央 2020-11-29 23:54

I have an extremely large JSON object structured like this:

{A : 1, B : 2, C : 3, D : 4}

I need a function that can swap the values with

18条回答
  •  星月不相逢
    2020-11-30 00:05

    With pure Ramda in a pure and point-free style:

    const swapKeysAndValues = R.pipe(
       R.toPairs,
       R.map(R.reverse),
       R.fromPairs,
    );
    

    Or, with a little more convoluted ES6 version, still pure functional:

    const swapKeysAndValues2 = obj => Object
        .entries(obj)
        .reduce((newObj, [key, value]) => ({...newObj, [value]: key}), {})
    

提交回复
热议问题