Search code examples
javascriptcheerio

How do you clone a cheerio object


I've got a cheerio object:

const $ = cheerio.load('<span class="layer-chunk"></span>')

This is a simple example it is far more complex.

I need to clone it so that I can do different things with it without actually effecting it.

This is what I've got so far:

const clone = $ => {
  const strHtml = $('body').html()
  return cheerio.load(strHtml)
}
const myClone = clone($)

But I am sure this is not a cheap op. There is a clone method in the docs but I cannot get it to work. I've tried this:

const myClone = $.root().clone()

But no cigar. Anyone know best practice for cloning a cheerio object? thanks


Solution

  • You can always just do:

    cheerio.load($.html())