我在http://verlager.com/demo.php
有一个例子此javascript:
读取并显示数据let dataString = "Worl, Seymour|Jones, Jim|Smith, Paul|Jolly, Roger|";
let splitString = dataString.split("|");
for (let i = 0; i < splitString.length;) {
$("#I" + i).val(splitString[i-1]); i++;
}
和支持json:
var standing = [
{"code" : "A", "rank" : "1"},
{"code" : "B", "rank" : "2"},
{"code" : "C", "rank" : "3"},
{"code" : "D", "rank" : "4"}
];
var members = [
{"Class": "A", "Name": "Jolly, Roger"},
{"Class": "B", "Name": "Smith, Paul"},
{"Class": "C","Name": "Jones, Jim"},
{"Class": "D","Name": "Worl, Seymour"}
];
我想要:“Worl,Seymour | Jones,Jim | Smith,Paul | Jolly,Roger |”
成为“Jolly,Roger | Smith,Paul | Jones,Jim | Worl,Seymour |”;
答案 0 :(得分:1)
根据您对代码运行方式的评论,我认为这将为您完成工作。我假设code
数组中的standing
与class
数组中的members
相同。在成员列表中找不到的名称将排在最底层。
let dataString = "Worl, Seymour|Jones, Jim|Smith, Paul|Jolly, Roger|";
let splitString = dataString.split("|");
var standing = [
{"code" : "A", "rank" : "1"},
{"code" : "B", "rank" : "2"},
{"code" : "C", "rank" : "3"},
{"code" : "D", "rank" : "4"}
];
var members = [
{"Class": "A", "Name": "Jolly, Roger"},
{"Class": "B", "Name": "Smith, Paul"},
{"Class": "C","Name": "Jones, Jim"},
{"Class": "D","Name": "Worl, Seymour"}
];
function getRank(name) {
var i;
for (i = 0; i < members.length; i++)
if (members[i].Name == name) break;
if (i == members.length) return 9999;
let code = members[i].Class;
for (i = 0; i < standing.length; i++)
if (standing[i].code == code) break;
if (i == standing.length) return 9999;
return parseInt(standing[i].rank);
}
function rank(name1, name2) {
return getRank(name1) - getRank(name2);
}
console.log(splitString.sort(rank).join('|'));
Jolly, Roger|Smith, Paul|Jones, Jim|Worl, Seymour|
答案 1 :(得分:0)
以下代码符合您的订购标准。我没有把琴弦放回原处;你的问题中没有足够的细节来解决潜在的角落案件。但我假设你真正需要的只是订购,你可以自己重新组装字符串。
let dataString = "Worl, Seymour|Jones, Jim|Smith, Paul|Jolly, Roger|";
var standing = [
{"code" : "A", "rank" : "1"},
{"code" : "B", "rank" : "2"},
{"code" : "C", "rank" : "3"},
{"code" : "D", "rank" : "4"}
];
var members = [
{"Class": "A", "Name": "Jolly, Roger"},
{"Class": "B", "Name": "Smith, Paul"},
{"Class": "C","Name": "Jones, Jim"},
{"Class": "D","Name": "Worl, Seymour"}
];
function getOrderFromClass(lhsClass, rhsClass) {
return getOrderFromCode(lhsClass, rhsClass);
}
function getOrderFromCode(lhsCode, rhsCode) {
var lhsRank, rhsRank;
for(let s of standing) {
if(s["code"] === lhsCode) { lhsRank = s["rank"]; }
if(s["code"] === rhsCode) { rhsRank = s["rank"]; }
}
if(lhsRank < rhsRank) { return -1; }
else if(lhsRank > rhsRank) { return 1; }
else { return 0; }
}
let splitString = dataString.split("|");
console.log(splitString);
splitString.sort(function(lhsName, rhsName) {
var lhsClass, rhsClass;
for(let m of members) {
if(m["Name"] === lhsName) { lhsClass = m["Class"]; }
if(m["Name"] === rhsName) { rhsClass = m["Class"]; }
}
return getOrderFromClass(lhsClass, rhsClass);
});
// All done
console.log(splitString);
答案 2 :(得分:0)
我将假设所有名称都有效,即存在于members数组中,以保持代码简单。
var standing = [
{ "code": "A", "rank": "1" },
{ "code": "B", "rank": "2" },
{ "code": "C", "rank": "3" },
{ "code": "D", "rank": "4" }
];
var members = [
{ "Class": "A", "Name": "Jolly, Roger" },
{ "Class": "B", "Name": "Smith, Paul" },
{ "Class": "C", "Name": "Jones, Jim" },
{ "Class": "D", "Name": "Worl, Seymour" }
];
const dataString = "Worl, Seymour|Jones, Jim|Smith, Paul|Jolly, Roger|";
// Filter out the empty string that split() produces at the end
const names = dataString.split("|").filter(name => name.length > 0);
// Map the names to members
const ms = names.map(name => members.find(m => m.Name === name));
// Map the members to standings
const ss = ms.map(m => standing.find(s => s.code === m.Class));
// Now fill the result with the names in the order of their ranking
const result = [];
names.forEach((name, i) => result[ss[i].rank - 1] = name);
for (let i = 0; i < result.length; i++) {
$("#I" + (i+1)).val(result[i]);
}
在更新DOM元素的最后一部分中,您似乎遇到了某种索引问题,因此我最好地猜测您要在那里做什么。但是,由于您尚未发布HTML,我不知道这是否完全正确。