简体   繁体   English

如何访问 JavaScript 中 function 中的变量?

[英]How to access variable inside a function in JavaScript?

I'm almost finishing my first project with React Native which is an expenditure app.我几乎用 React Native 完成了我的第一个项目,这是一个支出应用程序。

I'm building a chart from all my expenses in 2 currencies, the code is working I just have a small problem with accessing a local variable.我正在用两种货币的所有费用构建图表,代码正在运行我只是在访问局部变量时遇到了一个小问题。

I would like to access the 2 sum variable inside this function, so I can use it in my return for React Native to build a chart.我想访问这个 function 中的 2 sum变量,所以我可以在返回 React Native 时使用它来构建图表。

  // Get $ currency From fire base
  const q = query(collection(db, "users"),where("selected", "==", "Gasto"), where("moneda", "==", "$"));
  const unsubscribe = onSnapshot(q,(querySnapshot) => {
    const dolarCurrency = [];
    const months =[];
    querySnapshot.forEach((doc) =>{
      dolarCurrency.push(doc.data().cantidad);
      months.push(doc.data().fecha)
    })
    const hash = months.map((Day) => ({ Day }));
    const hashDolar = dolarCurrency.map(( Amount ) => ({ Amount }))

    const output = hash.map(({Day},i) => ({Day, ...hashDolar[i]}));
    
    /// I need this variable below
    const sum = output.reduce((acc, cur)=> {
      const found = acc.find(val => val.Day === cur.Day)
      if(found){
          found.Amount+=Number(cur.Amount)
      }
      else{
          acc.push({...cur, Amount: Number(cur.Amount)})
      }
      return acc
    }, [])
    })

  // Get C$ currency from Firebase
  const q2 = query(collection(db, "users"), where("selected", "==", "Gasto"),where("moneda", "==", "C$"));
  const unsubscribe2 = onSnapshot(q2, (querySnapshot) =>{
    const localCurrency = [];
    const months2 = [];
  querySnapshot.forEach((doc) => {
    localCurrency.push(doc.data().cantidad)
    months2.push(doc.data().fecha)
    })
  const hash = months2.map((Day) => ({ Day }));
  const hashLocalCurrency = localCurrency.map(( Amount ) => ({ Amount }))

  const output = hash.map(({Day},i) => ({Day, ...hashLocalCurrency[i]}));

  /// I need this variable below
  const sum = output.reduce((acc, cur)=> {
    const found = acc.find(val => val.Day === cur.Day)
    if(found){
        found.Amount+=Number(cur.Amount)
    }
    else{
        acc.push({...cur, Amount: Number(cur.Amount)})
    }
    return acc
  }, [])
  })

Any help or advice is welcome and appreciated!欢迎和赞赏任何帮助或建议!

You can either try state or ref, depends on whether UI part is to be updated, if UI is to be updated then state else ref does the job您可以尝试 state 或 ref,取决于是否要更新 UI 部分,如果要更新 UI,则 state 否则 ref 可以完成这项工作

const [sum,setSum] = useState(0) or const [sum,setSum] = useState(0) 或

const sumRef = useRef(0)常量 sumRef = useRef(0)

Then after that然后在那之后

 const sum = output.reduce((acc, cur)=> {
    const found = acc.find(val => val.Day === cur.Day)
    if(found){
        found.Amount+=Number(cur.Amount)
    }
    else{
        acc.push({...cur, Amount: Number(cur.Amount)})
    }
    return acc
  }, [])
  })

You can either setSum(sum) or sumRef.current = sum您可以setSum(sum)sumRef.current = sum

Hope it helps, feel free for doubts希望对你有帮助,有疑问欢迎交流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM