How to write an (if) statement in (map) function in dataviewjs

Things I have tried

  • This is the block of code in which I want to implement an “if” statement:
for (let group of dv.pages('"2 - Pillars"') 
.where(p => p.file.name != dv.current().file.name)
.groupBy(p => p.class)) 
{
    dv.header(2, group.key);

    dv.list(
        group.rows
            .sort(k => k.order, 'ASC')
            .map(k => k.symbol + " " +  k.file.link  + " :luc_arrow_big_left: " + k.status)
            )
}

I’ve tried the following modifications on the “map” function code line:

  • .map(k => k.symbol + " " + k.file.link + " :luc_arrow_big_left: " + if (k.status == "Manufacturing") {"🏗️"} else if (k.status == "Active") {"🟢"} else if (k.status == "Idle") {"🔴"} else {k.status})

But that didn’t work so I tried to declare status as a variable, after dv.header and before dv.list:

  • const status = k.status;

Then I performed the “forEach” function:

  • status.forEach(VI);

And finally modified the “map” line as following:

  • .map(k => k.symbol + " " + k.file.link + " :luc_arrow_big_left: " + function VI(item) { if (item == "Manufacturing") {"🏗️"} else if (item == "Active") {"🟢"} else if (item == "Idle") {"🔴"} else {item} })

That didn’t work either.

What I’m trying to do

As mentioned in the title I want to write an “if statement” in “map” function of a dataviewjs block to have the same result of “choice” in dataview blocks. I’m new to learning Javascript, but I’m still not able to work this out.

Thanks in advance;

Well, I’m a JS dumb (as in code in general)… because that take as “fragile” my observations about this subject.
But I suggest you solve that before the group step. For example:

const pages = dv.pages('"2 - Pillars"') 
.where(p => p.file.name != dv.current().file.name)

for (let page of pages){
	if (page.status == "Manufacturing"){
	page.status = "🏗️"
	} else if (page.status == "Active"){
	page.status = "🟢"
	} else if (page.status == "Idle"){
	page.status = "🔴"
	}
}

for (let group of pages.groupBy(p => p.class)){

    dv.header(2, group.key);

    dv.list(
        group.rows
            .sort(k => k.order, 'ASC')
            .map(k => k.symbol + " " +  k.file.link  + " 🪴 " + k.status)
            )
}

(replace " 🪴 " by your original emoji)

Instead of doing an if in the lambda function of map(), trying do a function call with the k.status as the parameter.

In other words make a function before your code:

function mapIcon(status) {
  if (status == "Active")
    return "A"
  else if (status == "Idle") 
    return "I"
  else
    return "?"
}

and let the .map be something like:

.map( k => k.symbol + " " + k.file.link + mapIcon(k.status) )

Code is untested and could surely be optimised, but the gist of it should be correct, and allow you to do whatever related to the k.status value.

Regards,
Holroy

That worked! Thank you so much!

I started learning JavaScript just to solve this specific problem, and I was still clueless after 39 lessons from some course. I really appreciate your help.

That also worked without any problems. Thank you so much, Holroy!