Looping through an array from Sanity in JS

3 replies
Last updated: Apr 1, 2020
This may be outisde the scope of Sanity help and more “JS” help, but any one know how I can loop through an array I get from Sanity?I’m trying this mehod now:

{data.map(({ node }) => console.log("Node data is: ", node))}

If I console log Data I get the JSON . but when I try to loop through it I get a console message stating “node is undefined”
If I just console.log(data) I get the array in the screenshot
Apr 1, 2020, 2:06 PM
maybe try removing the curly braces around node in the map callback
Apr 1, 2020, 2:08 PM
I was so sure I had already tried that 5 times, but now it worked. Thanks Mike!
Apr 1, 2020, 2:11 PM
of course! those curly braces you had initially are destructuring each object in the array. so for example, if you replaced node with auditTitle, you’d log out the value of each auditTitle
Apr 1, 2020, 2:15 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?