var replaceArr = ['A','J','Q',10,2];
var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];
按照代码片段,我有两个数组:
replaceArr
和originalArr
我想将
replaceArr
与originalArr
进行比较,并从那里获取键值,然后替换为replaceArr
。替换
replaceArr
之后的意思是== [0,10,11,9,1]
在高级谢谢..
最佳答案
您可以简单地使用数组原型中的map
和find
var replaceArr = ["A", "J","Q",10,2]
var originalArr = [{A:0},{2:1},{3:2},{4:3},{5:4},{6:5},{10:9},{J:10},{Q:11},{K:12}];
replaceArr = replaceArr.map(v => originalArr.find(obj => obj[v] !== undefined)[v]);
console.log(replaceArr);