javascript - get array value by index jquery - Stack Overflow

I have an javascript array that has the following values,let arr = ["2","3","4

I have an javascript array that has the following values,

let arr = ["2","3","4","5","6"];

I have a second array that has three integers,

let index = [1,3,4];

How would I use JQuery (or Javascript) to use the array to get the index from arr, and add it to a new array. I want to add these strings to a new array.

indexArr = ["3","5","6"]

Thanks

I have an javascript array that has the following values,

let arr = ["2","3","4","5","6"];

I have a second array that has three integers,

let index = [1,3,4];

How would I use JQuery (or Javascript) to use the array to get the index from arr, and add it to a new array. I want to add these strings to a new array.

indexArr = ["3","5","6"]

Thanks

Share Improve this question asked Jun 30, 2017 at 11:51 dark_dab0dark_dab0 2431 gold badge3 silver badges9 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 1

Simple, just use Array.prototype.map():

let arr = ["2","3","4","5","6"];
let index = [1,3,4];

let indexArr = index.map(i => arr[i]);

console.log(indexArr);

You can do it with simple .map

let arr = ["2","3","4","5","6"];
let index = [1,3,4];
index.map(x=>arr[x]) //["3", "5", "6"]

Just use a loop. Personally I prefer this method over map because I find this easier to understand, but it's personal preference.

let arr = ["2","3","4","5","6"];
let index = [1,3,4];
let indexArr = [];
for(var i=0; i<index.length; i++) {
    indexArr.push(arr[index[i]]);
}
console.log(indexArr);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744702467a4588852.html

相关推荐

  • javascript - get array value by index jquery - Stack Overflow

    I have an javascript array that has the following values,let arr = ["2","3","4

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信