我有一个数组,它包含教程中的每一步,并且它对应的git commit sha。我想选择每章的最后一步,对于以下示例,它对应于1.1
,2.5
和3.4
。我目前正在使用Typescript中的 let ... of 迭代器来分析步骤。
[1] step: '3.4' },
[1] Commit {
[1] hash: '52f7762e2a2d901b9d849e70679237fad8f0c05f',
[1] step: '3.3' },
[1] Commit {
[1] hash: '6ef63c21cd7f42fe5f190fb7bd5a7411528b944e',
[1] step: '3.2' },
[1] Commit {
[1] hash: 'fe7bd36851672a24c0cd205f0763d52e6abfa6e1',
[1] step: '3.1' },
[1] Commit {
[1] hash: '13b948cc246b3c9b383c4be24ca0ba0a7c072e67',
[1] step: '2.5' },
[1] Commit {
[1] hash: '679bc61d53a59ad10c0398f2faecd7a4052689dc',
[1] step: '2.4' },
[1] Commit {
[1] hash: '76f57e963afb885fed48b1646fd50025269061ac',
[1] step: '2.3' },
[1] Commit {
[1] hash: 'fd368fed4f47b9686e855b2a76e53dae5880ef69',
[1] step: '2.2' },
[1] Commit {
[1] hash: 'a70e6f556640db53f1ef3acba28c42f582d45890',
[1] step: '2.1' },
[1] Commit {
[1] hash: '95854756de842ff45ebbf9d3703cc7eff1557d5a',
[1] step: '1.1' },
这样做的方法是什么?
我目前的方法需要循环遍历所有提交以查找有多少章节,然后使用step = chapter.x
过滤掉所有结果。然后找到该数组中最大的x。然后重复所有章节。对于我想要实现的目标,这似乎非常笨拙。
答案 0 :(得分:0)
我会看一下像下划线max()这样的东西,它允许你传入数组,一个测试最大值的函数,并返回最大值。
如果这是一个项目,或者由于某种原因你不能使用库,那么一个简单的for循环就足够了。
答案 1 :(得分:0)
这是线性的最坏情况复杂,所以我能想到的最简单的方法(在简单的javascript中)是:
function maxChapters(commits) {
var result = [];
var recent = NaN;
for (var i = 0; i < commits.length; i++) {
var chapter = Math.floor(commits[i]['step']);
if (chapter != recent) {
result.push(commits[i]['step']);
recent = chapter;
}
}
return result;
}
var commits = [{
hash: '52f7762e2a2d901b9d849e70679237fad8f0c05f',
step: '3.3'
}, {
hash: '6ef63c21cd7f42fe5f190fb7bd5a7411528b944e',
step: '3.2'
}, {
hash: 'fe7bd36851672a24c0cd205f0763d52e6abfa6e1',
step: '3.1'
}, {
hash: '13b948cc246b3c9b383c4be24ca0ba0a7c072e67',
step: '2.5'
}, {
hash: '679bc61d53a59ad10c0398f2faecd7a4052689dc',
step: '2.4'
}, {
hash: '76f57e963afb885fed48b1646fd50025269061ac',
step: '2.3'
},{
hash: 'fd368fed4f47b9686e855b2a76e53dae5880ef69',
step: '2.2'
}, {
hash: 'a70e6f556640db53f1ef3acba28c42f582d45890',
step: '2.1'
}, {
hash: '95854756de842ff45ebbf9d3703cc7eff1557d5a',
step: '1.1'
}]
console.log(maxChapters(commits));
&#13;
答案 2 :(得分:0)
有很多方法可以解决这个问题。我使用节号来抵消数组中的索引。这节省了对数组的迭代(每章一次迭代而不是迭代每一步),以换取必须对每个元素进行更多操作(拆分步骤字符串并将步骤转换为int)。你的旅费可能会改变。与其他答案一样,这是一个简单的旧JavaScript解决方案。
//Only this function is the actual solution
function findLastSections(list) {
var chapterEnds = [];
var currCommit;
var currIndex = 0;
//Exit clause is when my index goes out-of-bounds.
while(currIndex < list.length) {
//I took the goal of trying to only iterate once per chapter.
console.log('processing element: ' + currIndex);
//get the last commit for the chapter
currCommit = list[currIndex];
//add its step to our return array.
chapterEnds.unshift(currCommit.step);
//split the step on the decimal, and parseInt so that I can do arithmetic on it. Add it to the index. This effectively shifts my position in the array by the length of the current chapter.
currIndex += parseInt(currCommit.step.split('.')[1]);
}
console.log('returning: ');
console.log(chapterEnds);
return chapterEnds;
}
//Everything below this line is just setup stuff to approximate your environment.
//Call to the function.
findLastSections(getCommitList());
// Constructor function in place of Typescript class
function Commit(hash, step) {
this.hash = hash;
this.step = step;
}
function getCommitList() {
//just did this to get it hoisted, so it's not crammed into the top of the snippet.
return [
new Commit (
'52f7362e2a2d901b9d849e70679237fad8f0c05f',
'3.4'),
new Commit (
'52f7762e2a2d901b9d849e70679237fad8f0c05f',
'3.3'),
new Commit (
'6ef63c21cd7f42fe5f190fb7bd5a7411528b944e',
'3.2'),
new Commit (
'fe7bd36851672a24c0cd205f0763d52e6abfa6e1',
'3.1'),
new Commit (
'13b948cc246b3c9b383c4be24ca0ba0a7c072e67',
'2.5'),
new Commit (
'679bc61d53a59ad10c0398f2faecd7a4052689dc',
'2.4'),
new Commit (
'76f57e963afb885fed48b1646fd50025269061ac',
'2.3'),
new Commit (
'fd368fed4f47b9686e855b2a76e53dae5880ef69',
'2.2'),
new Commit (
'a70e6f556640db53f1ef3acba28c42f582d45890',
'2.1'),
new Commit (
'95854756de842ff45ebbf9d3703cc7eff1557d5a',
'1.1')
];
}
&#13;