Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 咖喱有两种不同但相似的功能_Javascript - Fatal编程技术网

Javascript 咖喱有两种不同但相似的功能

Javascript 咖喱有两种不同但相似的功能,javascript,Javascript,我有两个不同但相似的函数来进行api调用。 我想知道有没有什么办法来咖喱他们 const getNameMatches = async partialName=> { const { data: documents} = await axios.get( `api/Documents/SearchDocument?searchString=${partialName}`, ); const { data: clients } = await axios.get('api

我有两个不同但相似的函数来进行api调用。 我想知道有没有什么办法来咖喱他们

const getNameMatches = async partialName=> {
  const { data: documents} = await axios.get(
    `api/Documents/SearchDocument?searchString=${partialName}`,
  );
  const { data: clients } = await axios.get('api/Clients/GetAll');
  return documents.map(document=> ({
    label: document.Name,
    subLabel: clients.find(c => c.id === document.clientID)?.firstName,
    value: document,
  }));

const getAddressMatches = async partialAddress=> {
  const { data: documents} = await axios.get(
    `api/Documents/SearchDocumentByAddress?searchString=${partialAddress}`,
  );
  const { data: clients } = await axios.get('api/Clients/GetAll');
  return documents.map(document=> ({
    label: document.propertyAddress,
    subLabel: clients.find(c => c.id === document.clientID)?.firstName,
    value: document,
  }));
我将这两个函数合并为一个函数,但我想对它们进行修饰,并想知道这是否可行

组合功能:

const getMatches = async (state, partialString) => {
  const tabValue = state.tabValue;
  const { data } = await axios.get(
    `api/Documents/SearchDocument${
      tabValue === 'Address' ? 'ByAddress' : ''
    }?searchString=${partialString}`,
  );
  const { data: clients } = await axios.get('api/Clients/GetAll');
  return data.map(loan => ({
    label: tabValue === 'Name' ? document.name: document.propertyAddress,
    subLabel: clients.find(c => c.id === loan.clientID)?.firstName,
    value: document,
  }));
};

唯一的区别是API URL和标签

因此,您可以收集它们并使其成为通用的

const getMatches = async (partialValue,type)=> {
  let urlString="",label="";
  if(type==='name'){
        urlString="SearchDocument";
        label="Name";
   }else{
        urlString="SearchDocumentByAddress";
        label="propertyAddress";
   }
  const { data: documents} = await axios.get(
    `api/Documents/${urlString}?searchString=${partialValue}`,
  );
  const { data: clients } = await axios.get('api/Clients/GetAll');
  return documents.map(document=> ({
    label: document[label],
    subLabel: clients.find(c => c.id === document.clientID)?.firstName,
    value: document,
  }));
如果你真的愿意,你可以这样做:

const searchFor = type => {
  const path = type === 'name'
    ? 'SearchDocument'
    : 'SearchDocumentByAddress';

  const prop = type === 'name'
    ? 'Name'
    : 'propertyAddress';

  return function(string){
    const apiDocs = `api/Documents/${path}?searchString=${string}`;
    const apiClients = `api/Clients/GetAll`;

    const documents = await axios.get(apiDocs);
    const clients = await axios.get(apiClients);

    return documents.map(document => ({
      label: document[prop],
      subLabel: clients.find(c => c.id === document.clientID).firstName,
      value: document
    }));
  }
}
然后你可以这样称呼它:

const getNameMatches = searchFor('name');
getNameMatches('foobar');

/* OR */

const getAddrMatches = searchFor('address');
getAddrMatches('foobar');

是的,这也是正确的。谢谢,但这不是我想要的。。。