【发布时间】:2021-05-10 17:36:59
【问题描述】:
我正在尝试从数组中删除位于另一个数组的当前索引之前的项。
例如我有这个:
const d = ["one", "two", "three", "four", "five", "six"];
const scenarioIndex = 1
const solution = () => {
return // return the solution here
};
在这种情况下,结果必须是["one"]
我总是必须返回当前和以前的项目。
如果const scenarioIndex = 2 必须返回["one", "two"]
我尝试了几件事但没有成功,这段代码似乎与我需要的相反 => https://codesandbox.io/s/javascript-forked-gjdoy?file=/index.js:0-263
const d = ["one", "two", "three", "four", "five", "six"];
const scenarioIndex = Math.floor(Math.random() * 5) + 0;
const solution = () => {
console.log(scenarioIndex);
return d.splice(scenarioIndex + 1, d.length - scenarioIndex);
};
console.log(solution());
【问题讨论】:
标签: javascript arrays ecmascript-6