gosseti
gosseti

Reputation: 975

Lift a key’s values to top level whilst preserving rest of object using Ramda

I would like to build it into my compose function so that record’s values go to the top level of the object, whilst the rest of the keys stay in tact:

{
  record: {
    seasons: [
      1
    ],
    colors: [
      2
    ]
  },
  tag_ids: [
    2091
  ]
}

The result I am after:

{
  seasons: [
    1
  ],
  colors: [
    2
  ],
  tag_ids: [
    2091
  ]
}

Any of the keys may or may not exist.

I have always scratched my head with the ramda way to do it in a compose function. Currently I am looking at toPairs and doing some pretty long winded transforms with no luck.

Upvotes: 0

Views: 192

Answers (4)

Hitmands
Hitmands

Reputation: 14199

This might help too!

const lift = key => R.converge(R.mergeRight, [
  R.dissoc(key),
  R.prop(key),
]);

const liftRecord = lift('record');

// ====
const data = {
  record: {
    seasons: [
      1
    ],
    colors: [
      2
    ]
  },
  tag_ids: [
    2091
  ]
};

console.log(
  liftRecord(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>

Upvotes: 1

Ori Drori
Ori Drori

Reputation: 193120

You can use R.chain with R.merge and R.prop to flatten a key's content by merging it with the original object, and then you can omit the original key.

const { pipe, chain, merge, prop, omit } = R

const fn = key => pipe(
  chain(merge, prop(key)), // flatten the key's content
  omit([key]) // remove the key
)

const data = { record: { seasons: [1], colors: [2] }, tag_ids: [2091] }

const result = fn('record')(data)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>

Upvotes: 1

Scott Christopher
Scott Christopher

Reputation: 6516

This may be simpler in plain JS rather than Ramda:

const data = { record: { seasons: [1], colors: [2] }, tag_ids: [2091] }

const flattenRecord = ({record = {}, ...rest}) => ({...record, ...rest})

flattenRecord(data) //=> {"colors": [2], "seasons": [1], "tag_ids": [2091]}

If you would still like to utilise Ramda for the solution, consider looking into R.mergeLeft (or R.mergeRight) and R.omit.

Upvotes: 3

Jacob
Jacob

Reputation: 926

You can use the spread operator.

const startingWithAllProps = {
  record: {
    seasons: [
      1
    ],
    colors: [
      2
    ]
  },
  tag_ids: [
    2091
  ]
}

const startingWithoutRecord = {
  tag_ids: [
    2091
  ]
}

const startingWithoutTagIds = {
  record: {
    seasons: [
      1
    ],
    colors: [
      2
    ]
  }
}

const moveRecordUpOneLevel = (startingObject) => {
  const temp = {
    ...startingObject.record,
    tag_ids: startingObject.tag_ids
  }
  return JSON.parse(JSON.stringify(temp)) // To remove any undefined props
}

const afterTransformWithAllProps = moveRecordUpOneLevel(startingWithAllProps)
const afterTransformWithoutRecord = moveRecordUpOneLevel(startingWithoutRecord)
const afterTransformWithoutTagIds = moveRecordUpOneLevel(startingWithoutTagIds)

console.log('afterTransformWithAllProps', afterTransformWithAllProps)
console.log('afterTransformWithoutRecord', afterTransformWithoutRecord)
console.log('afterTransformWithoutTagIds', afterTransformWithoutTagIds)

Upvotes: 1

Related Questions