我有几个参数的查询,我只想在不为空的情况下添加参数到查询中.

我唯一想到的是创建参数对象和有条件地添加属性,但 for each 查询添加这个感觉是错误的方式,因为我认为有更好的方法来处理这个 我的解决方案:

query: (category: string | null) => {
  const params: { [key: string]: string } = {};
  if (category !== null) params.category = category;

  return {
    url: "/products",
    params: params,
  };
},

推荐答案

您可以编写一个通用函数,在输入对象图形上执行深度/广度优先遍历,以修改(或复制+修改)它,以便删除空属性或设置为未定义.

我在RTKQuery的代码中使用了一个类似的函数来执行与后端API之间的串行化和串行化.


编辑:这是我的深度优先遍历实现的副本,用于遍历一个Java脚本对象图.它是通用的,可用于遍历前和遍历后.我已经用我自己的代码为它编写了许多单元测试,它工作得很好.

import { isArray } from "lodash";

export const DefaultRootName = "[root]";

export interface DepthFirstTraversalOptions<TCustom = any> {
  // if true (default), then the algorithm will iterate over any and all array entries
  traverseArrayEntries: boolean; 

  // pre (default) = visit the root before the children. post = visit all the children and then the root.
  order: "pre" | "post"; 

  // If order = "pre", then this function is used to determine if the children will be visited after
  //  the parent node. This parameter will be ignored if order != "pre".
  skipChildren?: (parent: Readonly<any>, context: Readonly<VisitContext<TCustom>>) => boolean;

  // You may optionally provide a name for the root. This name will appear as the first entry within
  // context.path. This doesn't effect the functionality, but it maybe helpful for debugging.
  rootName?: string;
}

export interface VisitContext<TCustom = any> {
  path: string[];
  options: DepthFirstTraversalOptions;
  visited: Set<any>;
  visitStack: any[];
  custom: TCustom;
}

// Clone the source node, and apply any transformations as needed.
export type VisitFunction<TCustom = any> 
  = (current: Readonly<any>, context: Readonly<VisitContext<TCustom>>) => void;

/**
 * Performs a Depth First Traversal over all of the objects in the graph, starting from <source>.
 * Properties are not visited in any particular order.
 * @param source 
 * @param visit 
 * @param options 
 */
export function depthFirstTraversal<TCustom = any>(source: any, visit: VisitFunction<TCustom>, 
  custom?: TCustom, options?: Partial<DepthFirstTraversalOptions<TCustom>>) {

  const realOptions: DepthFirstTraversalOptions<TCustom> = {
    traverseArrayEntries: true,
    order: "pre",
    ...options
  };

  const visited = new Set<any>([source]); 
  const visitStack = [source];
  const path: string[] = [realOptions.rootName ?? DefaultRootName];

  const ctx: VisitContext = {
    path,
    options: realOptions,
    visited,
    visitStack,
    custom
  };

  __DepthFirstTraversal<TCustom>(source, visit, ctx);
}

// performs a depth-first traversal of the source object, visiting every property.
// First the root/source is visited and then its child properties, in no particular order. 
function __DepthFirstTraversal<TCustom = any>(source: any, visit: VisitFunction<TCustom>, context: VisitContext<TCustom>) {

  // assume that the context has already been updated prior to this internal call being made
  if (context.options.order === "pre") {
    visit(source, context);
    if (context.options.skipChildren?.(source, context)) {
      return;
    }
  }

  // check to see if the source is a primitive type. If so, we are done.
  // NOTE: the source could be undefined/null. 
  if (Object(source) !== source) { 
    if (context.options.order === "post") {
      visit(source, context);
    }
    return;
  }

  if (!context.options.traverseArrayEntries && isArray(source)) {
    if (context.options.order === "post") {
      visit(source, context);
    }
    return;
  }

  // visit any child nodes
  Object.keys(source).forEach(field => {
    const curr = source[field];

    if (context.visited.has(curr)) { 
      // We have already traversed through it via some other reference
      return; 
    } if (Object(curr) === curr) {
      // it is not a primitive, and this is our first time traversing to it 
      // register it to prevent re-iterating over the same object in the event that there
      // is a loop in the object graph.
      context.visited.add(curr);
    }

    context.visitStack.push(curr);
    context.path.push(field);
    __DepthFirstTraversal(curr, visit, context);
    context.path.pop();
    context.visitStack.pop();
  });

  if (context.options.order === "post") {
    visit(source, context);
  }
}

export default depthFirstTraversal;

如果您希望使用它来遍历对象图并将所有空属性修改为未定义,则可以这样定义一个访问函数(注意:此位尚未经过测试/调试,可能需要特别注意数组条目):

const removeNullProperties:VisitFunction = 
(current, context) => {

  if(current !== null) { 
    return; 
  }

  // parent will be undefined if it is the root
  const parent = context.visitStack.at(-2); 
  const field = context.path.at(-1);

  parent[field] = undefined;
}; 

或者,您可以直接删除属性如下:delete parent[field];

你可以用我前面的函数将这个函数应用到你的输入对象图(params)上,如下所示:

depthFirstTraversal( params, removeNullProperties );

如果你喜欢复制修改params对象,那么你可以这样做:

const sanitizedParams = { ...params };
depthFirstTraversal( sanitizedParams, removeNullProperties );

Typescript相关问答推荐

在TypScript手册中可以视为接口类型是什么意思?

APP_INITIALIZER—TypeError:appInits不是函数

angular 17独立使用多个组件(例如两个组件)

TypeScript将键传递给新对象错误

将值添加到具有不同类型的对象的元素

Next.js错误:不变:页面未生成

有条件地删除区分的联合类型中的属性的可选属性

如何在TypeScrip中正确键入元素和事件目标?

笛卡尔产品类型

我可以使用typeof来强制执行某些字符串吗

如何在Angular 服务中制作同步方法?

如何在Deno中获取Base64图像的宽/高?

如何为带有参数的函数类型指定类型保护?

我的类型谓词函数不能像我预期的那样工作.需要帮助了解原因

换行TypeScript';s具有泛型类型的推断数据类型

确保财产存在

我可以使用对象属性的名称作为对象中的字符串值吗?

使用本机 Promise 覆盖 WinJS Promise 作为 Chrome 扩展内容脚本?

是否可以使用元组中对象的键来映射类型

如何为对象创建类型,其中键是只读且动态的,但值是固定类型?