如何从 map() 函数正确传递变量
How to properly pass variables from a map() function
我有以下代码,它根据 JSON 数据的两个不同部分创建变量。
function showDescription(implant) {
return implant.description + '<br />';
}
function showSectionHeadings(implant) {
return implant.sectionHeading + '<br />';
}
function loadImplantsOfFamily(implantFamily) {
var implantData = JSON.parse(getLocal("implantData"));
var allImplantsInFamily = implantData.implants.filter(familyToLoad,implantFamily);
var implantDescription = allImplantsInFamily.map(showDescription);
var sectionHeading = allImplantsInFamily.map(showSectionHeadings);
var uniqueVals = sectionHeading.unique();
$("#holderForImplants").html(uniqueVals);
}
一切正常,但我拥有的两个功能是相同的,除了它们所指的 JSON 键。如何将这两个函数合二为一,从 map 函数传入 JSON 变量?
这样的东西不起作用:
function showKey(implant,key) {
return implant.key + '<br />';
}
function loadImplantsOfFamily(implantFamily) {
var implantData = JSON.parse(getLocal("implantData"));
var allImplantsInFamily = implantData.implants.filter(familyToLoad,implantFamily);
var implantDescription = allImplantsInFamily.map(showKey("description"));
var sectionHeading = allImplantsInFamily.map(showKey("sectionHeading"));
var uniqueVals = sectionHeading.unique();
$("#holderForImplants").html(uniqueVals);
}
像下面这样简单地使用闭包:
function showKey(implant,key) {
return function(elem, index, arr){ //Catch 'elem', 'index', 'arr' - Element passed by Map function. Checkout the documentation for more on this.
return implant[key] + '<br />';
}
}
简单示例:
var a = {one:1, two:2}
var b = {three:3, four: 4};
var dynamic = function(obj, key){
return function(el){ //Closure call to save the key. 'el' will be element injected from Map function
console.log(obj[key])
return obj[key];
}
}
var test = [1, 2, 3];
test.map(dynamic(b, "four"));
test.map(dynamic(a, "one"));
https://jsfiddle.net/mdLda62f/
闭包函数可帮助您将动态键保存在函数引用中,以便在运行时提取所需的值。这就是 javascript 闭包概念的美妙之处。
参考
有关 Array.Map 的文档,用于对 map 函数及其参数进行更多解释
您可以对对象属性使用括号表示法,以便您可以使用变量。您的函数看起来像这样:
function showDescription(implant, key) {
return implant[key] + '<br />';
}
然后这样称呼它:
showDescription(implant, 'description');
showDescription(implant, 'sectionHeading');
如果你想结合这个功能,你可以使用闭包,按照下面的方式实现功能。
function showKey(prop){
return function(obj){
return obj[prop];
};
}
在稍后阶段,只需调用您在代码中描述的 showKey
函数。内部函数将由 map
调用,通过传递数组元素,闭包将确保访问适当的 属性 名称。
我有以下代码,它根据 JSON 数据的两个不同部分创建变量。
function showDescription(implant) {
return implant.description + '<br />';
}
function showSectionHeadings(implant) {
return implant.sectionHeading + '<br />';
}
function loadImplantsOfFamily(implantFamily) {
var implantData = JSON.parse(getLocal("implantData"));
var allImplantsInFamily = implantData.implants.filter(familyToLoad,implantFamily);
var implantDescription = allImplantsInFamily.map(showDescription);
var sectionHeading = allImplantsInFamily.map(showSectionHeadings);
var uniqueVals = sectionHeading.unique();
$("#holderForImplants").html(uniqueVals);
}
一切正常,但我拥有的两个功能是相同的,除了它们所指的 JSON 键。如何将这两个函数合二为一,从 map 函数传入 JSON 变量?
这样的东西不起作用:
function showKey(implant,key) {
return implant.key + '<br />';
}
function loadImplantsOfFamily(implantFamily) {
var implantData = JSON.parse(getLocal("implantData"));
var allImplantsInFamily = implantData.implants.filter(familyToLoad,implantFamily);
var implantDescription = allImplantsInFamily.map(showKey("description"));
var sectionHeading = allImplantsInFamily.map(showKey("sectionHeading"));
var uniqueVals = sectionHeading.unique();
$("#holderForImplants").html(uniqueVals);
}
像下面这样简单地使用闭包:
function showKey(implant,key) {
return function(elem, index, arr){ //Catch 'elem', 'index', 'arr' - Element passed by Map function. Checkout the documentation for more on this.
return implant[key] + '<br />';
}
}
简单示例:
var a = {one:1, two:2}
var b = {three:3, four: 4};
var dynamic = function(obj, key){
return function(el){ //Closure call to save the key. 'el' will be element injected from Map function
console.log(obj[key])
return obj[key];
}
}
var test = [1, 2, 3];
test.map(dynamic(b, "four"));
test.map(dynamic(a, "one"));
https://jsfiddle.net/mdLda62f/
闭包函数可帮助您将动态键保存在函数引用中,以便在运行时提取所需的值。这就是 javascript 闭包概念的美妙之处。
参考
有关 Array.Map 的文档,用于对 map 函数及其参数进行更多解释
您可以对对象属性使用括号表示法,以便您可以使用变量。您的函数看起来像这样:
function showDescription(implant, key) {
return implant[key] + '<br />';
}
然后这样称呼它:
showDescription(implant, 'description');
showDescription(implant, 'sectionHeading');
如果你想结合这个功能,你可以使用闭包,按照下面的方式实现功能。
function showKey(prop){
return function(obj){
return obj[prop];
};
}
在稍后阶段,只需调用您在代码中描述的 showKey
函数。内部函数将由 map
调用,通过传递数组元素,闭包将确保访问适当的 属性 名称。