如何将布尔值转换为字符串 - NestJS / JS / Node

问题描述 投票:0回答:2

我是 NestJS 作为服务器的新手。 我在数据库中有一个正在查询的表,列名是优先级,在数据库中它是布尔值,只有 0 或 1。
我有这段代码,我理解将数据映射到 DTO。

forMember(
          (destination) => destination.precedence,
          mapFrom((source) => source.precedence),
        ),

但我需要的是 true 而不是 1 我需要它是 false,而不是 0

可以吗?在映射器中?

javascript node.js nestjs mapper
2个回答
1
投票

你只需检查

source.precedence
是否等于 '1' 然后返回
true
否则返回
false

forMember(
 (destination) => destination.precedence,
 mapFrom((source) => source.precedence === 1), // this will return false also if the value is different from 0 and 1
),

0
投票

您可以使用双重否定将假值或真值转换为布尔值

console.log(!!0)
console.log(!!1)
// and just convert your boolean to string if you need to
console.log((!!0).toString())
console.log((!!1).toString())

© www.soinside.com 2019 - 2024. All rights reserved.