admin管理员组文章数量:1023857
How can I return a range of elements in array like so, without using a For Loop, ForEach Statement, etc.
var array = ["1", "2", "3"]
console.log(array[0-3]);
//result
//1
//2
//3
How can I return a range of elements in array like so, without using a For Loop, ForEach Statement, etc.
var array = ["1", "2", "3"]
console.log(array[0-3]);
//result
//1
//2
//3
Share
Improve this question
asked Oct 13, 2019 at 5:46
user11077261user11077261
3 Answers
Reset to default 3You can use slice
var array = ["1", "2", "3"]
let indexRange = (arr, start, end) => {
return arr.slice(start, end)
}
console.log(indexRange(array, 0, 3));
If your range is string then you can use split and slice
var array = ["1", "2", "3"]
let indexRange = (arr, range) => {
let [start,end] = range.split('-').map(Number)
return arr.slice(start, end)
}
console.log(indexRange(array, "0-3"));
use slice() method. slice() method returns the selected elements in an array, as a new array object.
SYNTAX:
array.slice(start, end)
var array = ["1", "2", "3"]
console.log(array.slice(0,3));
//result
//1
//2
//3
You can create a function & then pass the range as a string. Inside the function body split the range by the delimiter -
. Then join the elements of the array and create a string. Use substr
to get the element between the range. This will create a new string & while returning again split it to create an array
var array = ["1", "2", "3", "4", "5"]
function getElemInRange(range) {
let getRange = range.split('-');
return array.join('')
.substr(parseInt(getRange[0], 10), parseInt(getRange[1], 10))
.split('');
}
console.log(getElemInRange('0-3'));
How can I return a range of elements in array like so, without using a For Loop, ForEach Statement, etc.
var array = ["1", "2", "3"]
console.log(array[0-3]);
//result
//1
//2
//3
How can I return a range of elements in array like so, without using a For Loop, ForEach Statement, etc.
var array = ["1", "2", "3"]
console.log(array[0-3]);
//result
//1
//2
//3
Share
Improve this question
asked Oct 13, 2019 at 5:46
user11077261user11077261
3 Answers
Reset to default 3You can use slice
var array = ["1", "2", "3"]
let indexRange = (arr, start, end) => {
return arr.slice(start, end)
}
console.log(indexRange(array, 0, 3));
If your range is string then you can use split and slice
var array = ["1", "2", "3"]
let indexRange = (arr, range) => {
let [start,end] = range.split('-').map(Number)
return arr.slice(start, end)
}
console.log(indexRange(array, "0-3"));
use slice() method. slice() method returns the selected elements in an array, as a new array object.
SYNTAX:
array.slice(start, end)
var array = ["1", "2", "3"]
console.log(array.slice(0,3));
//result
//1
//2
//3
You can create a function & then pass the range as a string. Inside the function body split the range by the delimiter -
. Then join the elements of the array and create a string. Use substr
to get the element between the range. This will create a new string & while returning again split it to create an array
var array = ["1", "2", "3", "4", "5"]
function getElemInRange(range) {
let getRange = range.split('-');
return array.join('')
.substr(parseInt(getRange[0], 10), parseInt(getRange[1], 10))
.split('');
}
console.log(getElemInRange('0-3'));
本文标签: javascriptreturn an index range of elements in an arrayStack Overflow
版权声明:本文标题:javascript - return an index range of elements in an array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745556068a2155882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论