简体   繁体   English

如何替换javascript中的嵌套箭头功能以实现Safari兼容性?

[英]How to replace nested arrow functions in javascript for Safari compatibility?

I am customizing a JavaScript visualization library and have written a small piece of code to fill an array for the hover info: 我正在自定义JavaScript可视化库,并编写了一小段代码来填充悬停信息数组:

var text = zValues.map((zValues, i) => zValues.map((value, j) => {
  return ` ID: ${yValues[i]}<br> Tissue: ${xValues[j]}<br> Expression: ${value.toFixed(2)} ` 
 }))

It works for all browsers except Safari. 它适用于除Safari之外的所有浏览器。 I read that Safari does not support the arrow function, so I tried to replace this function as follows: 我读到Safari不支持箭头功能,因此我尝试如下替换此功能:

var text = zValues.map (function(zValues, i) { zValues.map (function (value, j) {
  return ` ID: ${yValues[i]}<br> Tissue: ${xValues[j]}<br> Expression: ${value.toFixed(2)} ` 
 });});

This does not give any error, but leads to an empty 'text' array. 这不会产生任何错误,但是会导致一个空的“文本”数组。 How can I fix the problem? 我该如何解决该问题?

Arrow functions with just a single expression statement in the body have an implicit return. 主体中只有一个表达式语句的Arrow函数具有隐式返回。 Functions created with function never have an implicit return (unless you count undefined I guess), so you need to explicitly return your arrow function expressions: 用函数创建的function 永远不会有隐式的返回(除非我猜除非是undefined ),所以您需要显式返回箭头函数表达式:

var text = zValues.map (function(zValues, i) { return zValues.map (function (value, j) {
  return ` ID: ${yValues[i]}<br> Tissue: ${xValues[j]}<br> Expression: ${value.toFixed(2)} ` 
 });});

(You already had the return on the innermost result expression.) (您已经在最里面的结果表达式上得到了return 。)

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

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