Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse multiple div with same class name in a div in cheerio?

I have "n" number of classes with className: "classparent"
In which I have "n" number of classes with className: "class1"
which consists of "n" number of div’s with className: "class2"

How can I parse each and every of these div.class2 and get their style property in cheerio ???

Currently I am doing this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$(".classParent").each((i, el) => {
    prop[i] = $(el).find(".class1 .class2").attr("style")
})

It returns me only one div.class2 from every .class1.

I want results like this:

[
 {}, // 1st object which contains all style properties of .class2 of 1st .class1
 {}, // 2nd object which contains all style properties of .class2 of 2nd .class1
 {}, // 3rd object which contains all style properties of .class2 of 3rd .class1
 ...
]

And this is how my objects would look like:

{
 "style attribute value",
 "style attribute value",
 "style attribute value",
 ......
}

>Solution :

You can use the toArray function:

$(".classParent").each((i, el) => {
    prop[i] = $(el)
        .find(".class1 .class2")
        .toArray()
        .map($)
        .map(d => d.attr("style"));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading