You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将布尔值转换为数值:如何实现更简洁高效的算法?

简化布尔值转数值的嵌套数据转换函数实现

需求说明

实现一个可接收任意数据类型的函数booleanToInt,将输入中所有布尔类型转换为对应数值(true转为1,false转为0),支持任意嵌套层级的对象、数组、字符串、数字等参数。示例如下:

booleanToInt('qwerty') // 'qwerty' 
booleanToInt(1) // 1 
booleanToInt(false) // 0 
booleanToInt(true) // 1 
booleanToInt([1, 'qwerty', false]) // [1, 'qwerty', 0] 
booleanToInt([1, 'qwerty', { a: true }]) // [1, 'qwerty', { a: 1 }] 
booleanToInt({ a: { b: true }, c: false, d: 'qwerty' }) // { a: { b: 1 }, c: 0, d: 'qwerty' } 

原实现代码

// Checking for an object
function isObject(value) {
  return typeof value === "object" && !Array.isArray(value) && value !== null;
}

// Transforming an object
function transformObject(obj) {
  Object.keys(obj).forEach((key) => {
    if (isObject(obj[key])) {
      transformObject(obj[key]);
    } else if (Array.isArray(obj[key])) {
      transformArray(obj[key]);
    } else {
      obj[key] = transformStatic(obj[key]);
    }
  });
}

// Transforming the array
function transformArray(list) {
  list.forEach((item, i) => {
    if (isObject(item)) {
      transformObject(item);
    } else if (Array.isArray(item)) {
      transformArray(item);
    } else {
      list[i] = transformStatic(item);
    }
  });
}

// Transforming Static Types
function transformStatic(value) {
  let res = value;

  if (typeof value === "boolean") {
    res = +value;
  }

  return res;
}

// main function
function booleanToInt(value) {
  if (isObject(value)) {
    transformObject(value);
    return value;
  }

  if (Array.isArray(value)) {
    transformArray(value);
    return value;
  }

  return transformStatic(value);
}

console.log(booleanToInt("qwerty")); // 'qwerty'
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1

console.log(booleanToInt([1, "qwerty", false])); // [1, 'qwerty', 0]
console.log(booleanToInt([1, "qwerty", { a: true }])); //[1, 'qwerty', { a: 1 }]

原实现被反馈逻辑过于繁琐,以下是更简洁高效的替代方案:

简化实现方案

纯函数版本(无副作用,返回新对象/数组)

通过递归合并所有类型判断逻辑,用原生API简化对象和数组的遍历转换:

function booleanToInt(value) {
  // 布尔值直接转数值
  if (typeof value === 'boolean') {
    return +value;
  }
  // 数组递归处理每个元素
  if (Array.isArray(value)) {
    return value.map(item => booleanToInt(item));
  }
  // 对象(排除null)递归处理每个属性
  if (typeof value === 'object' && value !== null) {
    return Object.fromEntries(
      Object.entries(value).map(([key, val]) => [key, booleanToInt(val)])
    );
  }
  // 其他类型直接返回
  return value;
}

// 测试验证
console.log(booleanToInt('qwerty')); // 'qwerty'
console.log(booleanToInt(1)); // 1
console.log(booleanToInt(false)); // 0
console.log(booleanToInt(true)); // 1
console.log(booleanToInt([1, 'qwerty', false])); // [1, 'qwerty', 0]
console.log(booleanToInt([1, 'qwerty', { a: true }])); // [1, 'qwerty', { a: 1 }]
console.log(booleanToInt({ a: { b: true }, c: false, d: 'qwerty' })); // { a: { b: 1 }, c: 0, d: 'qwerty' }

原地修改版本(和原实现行为一致)

如果需要保持原实现的原地修改特性,可调整为:

function booleanToInt(value) {
  if (typeof value === 'boolean') {
    return +value;
  }
  if (Array.isArray(value)) {
    value.forEach((item, index) => {
      value[index] = booleanToInt(item);
    });
    return value;
  }
  if (typeof value === 'object' && value !== null) {
    Object.keys(value).forEach(key => {
      value[key] = booleanToInt(value[key]);
    });
    return value;
  }
  return value;
}

简化逻辑说明

  1. 合并冗余函数:用单一递归函数处理所有类型,无需拆分对象、数组、静态类型的独立转换函数,减少代码量。
  2. 原生API简化操作:用Object.entries+Object.fromEntries快速处理对象属性转换,比手动遍历Object.keys更简洁。
  3. 逻辑清晰:按「布尔值→数组→对象→其他类型」的顺序判断,层级明确易读。

内容的提问来源于stack exchange,提问作者magistr4815

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:46:09