我已经列出了它所代表的船只清单:
var fleet =
["RMS MARY", 2000, 15],
["TITANIC 2", 10000, 13],
["Boaty McBoatface", 2000, 18],
["Jutlandia", 1945, 10],
["Hjejlen", 250, 8]
];
我想编写一个按给定容量过滤船只的功能。 例如:
filterByCapacity(fleet,5000)
这应该只返回Titanic 2
号船只,因为它是唯一容量高于5000
的船只。
关于如何编写这样一个函数的任何想法?
答案 0 :(得分:1)
简单:
System.TypeInitializationException: The type initializer for '<StartupCode$Company-EFeedbackExport>.$Repository' threw an exception. ---> System.ArgumentOutOfRangeException: No mapping exists from SqlDbType Date to a known DbType.
Parameter name: SqlDbType
at System.Data.SqlClient.SqlParameter.SetSqlDbType (SqlDbType type) <0x411a3080 + 0x00afb> in <filename unknown>:0
at System.Data.SqlClient.SqlParameter.set_SqlDbType (SqlDbType value) <0x411a3040 + 0x00017> in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlParameter:set_SqlDbType (System.Data.SqlDbType)
at FSharp.Data.Sql.Providers.MSSqlServer.getDbType@27 (Int32 providerType) <0x411a17c0 + 0x0008b> in <filename unknown>:0
at FSharp.Data.Sql.Providers.MSSqlServer+mappings@38.GenerateNext (IEnumerable`1& next) <0x411999f0 + 0x007bb> in <filename unknown>:0
at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].MoveNextImpl () <0x41199790 + 0x00069> in <filename unknown>:0
at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].System-Collections-IEnumerator-MoveNext () <0x41199760 + 0x00017> in <filename unknown>:0
at Microsoft.FSharp.Collections.SeqModule.ToList[T] (IEnumerable`1 source) <0x411991c0 + 0x001ea> in <filename unknown>:0
at FSharp.Data.Sql.Providers.MSSqlServer.createTypeMappings (IDbConnection con) <0x411761d0 + 0x00097> in <filename unknown>:0
at FSharp.Data.Sql.Providers.MSSqlServerProvider.FSharp-Data-Sql-Common-ISqlProvider-CreateTypeMappings (IDbConnection con) <0x411761a0 + 0x00013> in <filename unknown>:0
at <StartupCode$FSharp-Data-SqlProvider>.$SqlRuntime.DataContext.f@1-29 (System.String runtimeAssembly, System.String resolutionPath, System.String[] referencedAssemblies, DatabaseProviderTypes providerType, System.String owner, FSharp.Data.Sql.Runtime.SqlDataContext this, Microsoft.FSharp.Core.Unit unitVar0) <0x401883d0 + 0x00109> in <filename unknown>:0
at FSharp.Data.Sql.Runtime.SqlDataContext..ctor (System.String typeName, System.String connectionString, DatabaseProviderTypes providerType, System.String resolutionPath, System.String[] referencedAssemblies, System.String runtimeAssembly, System.String owner, CaseSensitivityChange caseSensitivity) <0x401880b0 + 0x00153> in <filename unknown>:0
at <StartupCode$Company-EFeedbackExport>.$Repository..cctor () <0x40187650 + 0x002ab> in <filename unknown>:0
--- End of inner exception stack trace ---
at Program.main (System.String[] args) <0x4016de00 + 0x00087> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for '<StartupCode$Company-EFeedbackExport>.$Repository' threw an exception. ---> System.ArgumentOutOfRangeException: No mapping exists from SqlDbType Date to a known DbType.
Parameter name: SqlDbType
at System.Data.SqlClient.SqlParameter.SetSqlDbType (SqlDbType type) <0x411a3080 + 0x00afb> in <filename unknown>:0
at System.Data.SqlClient.SqlParameter.set_SqlDbType (SqlDbType value) <0x411a3040 + 0x00017> in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Data.SqlClient.SqlParameter:set_SqlDbType (System.Data.SqlDbType)
at FSharp.Data.Sql.Providers.MSSqlServer.getDbType@27 (Int32 providerType) <0x411a17c0 + 0x0008b> in <filename unknown>:0
at FSharp.Data.Sql.Providers.MSSqlServer+mappings@38.GenerateNext (IEnumerable`1& next) <0x411999f0 + 0x007bb> in <filename unknown>:0
at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].MoveNextImpl () <0x41199790 + 0x00069> in <filename unknown>:0
at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[T].System-Collections-IEnumerator-MoveNext () <0x41199760 + 0x00017> in <filename unknown>:0
at Microsoft.FSharp.Collections.SeqModule.ToList[T] (IEnumerable`1 source) <0x411991c0 + 0x001ea> in <filename unknown>:0
at FSharp.Data.Sql.Providers.MSSqlServer.createTypeMappings (IDbConnection con) <0x411761d0 + 0x00097> in <filename unknown>:0
at FSharp.Data.Sql.Providers.MSSqlServerProvider.FSharp-Data-Sql-Common-ISqlProvider-CreateTypeMappings (IDbConnection con) <0x411761a0 + 0x00013> in <filename unknown>:0
at <StartupCode$FSharp-Data-SqlProvider>.$SqlRuntime.DataContext.f@1-29 (System.String runtimeAssembly, System.String resolutionPath, System.String[] referencedAssemblies, DatabaseProviderTypes providerType, System.String owner, FSharp.Data.Sql.Runtime.SqlDataContext this, Microsoft.FSharp.Core.Unit unitVar0) <0x401883d0 + 0x00109> in <filename unknown>:0
at FSharp.Data.Sql.Runtime.SqlDataContext..ctor (System.String typeName, System.String connectionString, DatabaseProviderTypes providerType, System.String resolutionPath, System.String[] referencedAssemblies, System.String runtimeAssembly, System.String owner, CaseSensitivityChange caseSensitivity) <0x401880b0 + 0x00153> in <filename unknown>:0
at <StartupCode$Company-EFeedbackExport>.$Repository..cctor () <0x40187650 + 0x002ab> in <filename unknown>:0
--- End of inner exception stack trace ---
at Program.main (System.String[] args) <0x4016de00 + 0x00087> in <filename unknown>:0
但我建议您使用对象而不是数组。更像是这样:
function filterByCapacity(fleet, capacity) {
var filteredArray = [];
for (var i = 0; i < fleet.length; i++) {
// Supposing we know that the capacity is the second index in the array
if (fleet[i][1] >= capacity) // Or you can make this strictly greather than (>)
filteredArray.push(fleet[i]);
}
return (filteredArray);
}
这样你可以制作一个通用函数var fleet = [
{
name: "RMS MARY",
capacity: 2000,
age: 15, // I had no idea what the third index meant so I made up one
},
{
name: "TITANIC 2",
capacity: 10000,
age: 13,
},
{
name: "Boaty McBoatface",
capacity: 2000,
age: 18,
},
{
name: "Jutlandia",
capacity: 1945,
age: 10,
},
{
name: "Hjejlen",
capacity: 250,
age: 8,
}
];
,它可以返回与过滤器相匹配的船只
答案 1 :(得分:1)
首先,我想指出你的阵列制作得很糟糕。您在第一个条目(“RMS MARY”)上缺少左方括号。更正后的数组如下:
var fleet = [
["RMS MARY", 2000, 15],
["TITANIC 2", 10000, 13],
["Boaty McBoatface", 2000, 18],
["Jutlandia", 1945, 10],
["Hjejlen", 250, 8],
];
其次,我建议您使用对象代替数组,而不是数组,所以你有这样的东西:
var fleet = {
{name: "RMS MARY", capacity: 2000, somethingElse: 15},
{name: "TITANIC 2", capacity: 10000, somethingElse: 13},
{name: "Boaty McBoatface", capacity: 2000, somethingElse: 18},
{name: "Jutlandia", capacity: 1945, somethingElse: 10},
{name: "Hjejlen", capacity: 250, somethingElse: 8},
};
现在,对于你想要的函数,我将提供两个,一个用于船只的原始数组表示,一个用于对象表示:
//array representation
function filterByCapacity(fleet, capacity){
return fleet.filter(function(ship){
return ship[1] >= capacity;
});
}
//object representation
function filterByCapacity(fleet, capacity){
return fleet.filter(function(ship){
return ship.capacity >= capacity;
});
}
希望这有帮助。
答案 2 :(得分:0)
将您的船舶表示为物体:
var fleet = [
{name: "RMS Mary", capacity: 2000, whatever: 15},
...
];
然后,您可以以可读且有意义的方式使用过滤器。
function filterByCapacity(fleetArr, minCapacity) {
return fleetArr.filter(function(ship) {
return ship.capacity > minCapacity;
});
}
console.log(filterByCapacity(fleet, 5000)) // only Titanic