javascript - 根据不同数组的 array.sort() 对一个数组进行排序
问题描述
从基于 CSV 文件创建的数组中,我对索引 0 进行排序。 var headerArray = allText[0].split(",").sort()
我需要重新排序 allText 的所有后续索引以匹配 headerArray 的索引,以便正确映射对象。
下面的代码有两个功能。第一个功能是创建对象。第二个函数是验证标头,并创建一个字符串数组,该数组将在第一个函数中用于将值映射到对象属性 (arrOfIndices)。
//Global variable
var arrOfIndices;
function createFileObjects(allText) {
validateHeaders(allText[0]);
var fileMap = {}
for(var i = 1; i < allText.length; i++) {
var currentParams = allText[i].split(",");
for (var j = 0; j < arrOfIndices.length; j++) {
fileMap[arrOfIndices[j]] = currentParams[j];
}
}
}
function validateHeaders(headerString) {
var headerPermutations = function (sealPerms, appNamePerms, entityIdPerms, entityNamePerms, entityTypePerms, envPerms, logIdPerms, sidPerms, userNamePerms, userStatusPerms, entlNamePerms, entlDescPerms, fidOwnerSidPerms, fidOwnerNamePerms, lmSidPerms, lmNamePerms) {
this.sealPerms = sealPerms;
this.appNamePerms = appNamePerms;
this.entityIdPerms = entityIdPerms;
this.entityNamePerms = entityNamePerms;
this.entityTypePerms = entityTypePerms;
this.envPerms = envPerms;
this.logIdPerms = logIdPerms;
this.sidPerms = sidPerms;
this.userNamePerms = userNamePerms;
this.userStatusPerms = userStatusPerms;
this.entlNamePerms = entlNamePerms;
this.entlDescPerms = entlDescPerms;
this.fidOwnerSidPerms = fidOwnerSidPerms;
this.fidOwnerNamePerms = fidOwnerNamePerms;
this.lmSidPerms = lmSidPerms;
this.lmNamePerms = lmNamePerms;
}
var seal = ['seal', 'id', 'seel', 'identification', 'app', 'application', 'identifier'];
var appName = ['app', 'application', 'appl', 'applic', 'name', 'nm'];
var entId = ['entity', 'ent', 'id', 'identifier'];
var entName = ['entity', 'ent', 'name'];
var entType = ['entity', 'ent', 'type'];
var env = ['env', 'environment'];
var logId = ['log', 'login', 'id', 'identifier'];
var sid = ['sid', 'standard', 'id'];
var userName = ['username', 'user', 'name', 'employee'];
var userStat = ['status', 'user', 'employee', 'activity', 'active'];
var entlName = ['entitlement', 'entl', 'name'];
var entlDesc = ['entitlement', 'entl', 'description', 'desc'];
var fidSid = ['fid', 'owner', 'sid', 'functional', 'id', 'standard'];
var fidName = ['fid', 'owner', 'function', 'id', 'name'];
var lmSid = ['lm', 'line', 'manager', 'sid', 'standard', 'id', 'identifier'];
var lmName = ['lm', 'line', 'manager', 'name'];
arrOfPermutations.push(seal, appName, entId, entName, entType, env, logId, sid, userName, userStat, entlName, entlDesc, fidSid, fidName, lmSid, lmName);
var arrOfBools = [];
var sortedArr = string.split(",").sort;
for (var i = 0; i < sortedArr.length; i++) {
for (var j = 0; j < arrOfPermutations.length; j++) {
var booleanTest = match(sortedArr[i], arrOfPermutations[j]);
if (booleanTest) {
switch (j) {
case 0: arrOfIndices.push("sealId");
break;
case 1: arrOfIndices.push("applicationNameInSeal");
break;
case 2: arrOfIndices.push("entityId");
break;
case 3: arrOfIndices.push("entityName");
break;
case 4: arrOfIndices.push("entityType");
break;
case 5: arrOfIndices.push("environment");
break;
case 6: arrOfIndices.push("loginId");
break;
case 7: arrOfIndices.push("userSid");
break;
case 8: arrOfIndices.push("userName");
break;
case 9: arrOfIndices.push("userStatus");
break;
case 10: arrOfIndices.push("userType");
break;
case 11: arrOfIndices.push("entitlementName");
break;
case 12: arrOfIndices.push("entitlementDesc");
break;
case 13: arrOfIndices.push("fidOwnerSid");
break;
case 14: arrOfIndices.push("fidOwnerName");
break;
case 15: arrOfIndices.push("lmSid");
break;
case 16: arrOfIndices.push("lmName");
break;
}
arrOfBools.push(true);
}
}
}
if (arrOfBools.length == sortedArr.length) {
//return arrOfIndices
return "File Format Correct";
} else { return "File Format Incorrect";}
}
预期结果是对象根据已排序的 headerArray 正确映射。
解决方案
如果有人好奇,我想出了一个答案。(也许它可以帮助别人?)
for (var i = 0; i < originalArr.length; i++) {
for (var j = 0; j < sortedArr.length; j++) {
if (originalArr[i] == sortedArr[j]) {
arrOfIndexes.push(j);
}
}
}
var fileMap = {}
var currentSorted = [];
for (k = 0; k < allText.length; k++) {
currentSorted.push(' ');
}
for (var i = 1; i < allText.length; i++) {
var currentParams = allText[i].split(",");
for (var j = 0; j < currentParams.length; j++) {
currentSorted[arrOfIndexes[j]] = currentParams[j];
}
allTextSorted.push(currentSorted.join());
}
推荐阅读
- javascript - 限制 chrome 扩展中的谷歌地图 api 键
- css - 如何将元素始终放在具有变换属性的图像的顶部
- python - 如何在 sklearn.neighbors 函数中获取距离
- javascript - 在 ReactJS 中调用功能组件相关的问题
- java - 向axis1.4客户端java添加隐含soap头
- c# - 尝试在 MVC 控制器中调用 json ID 但我收到此错误“对象引用未设置为对象的实例”
- python - 将html转换为纯文本Python
- c++ - 为什么浮点转换不起作用?
- c++ - 在 SDL2、C++ 中渲染时查找文本纹理中最后一个字符的坐标
- apache-kafka - Debezium - MySQL 连接器 - Kinesis - 服务未启动