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}];





按照代码片段,我有两个数组:
replaceArroriginalArr

我想将replaceArroriginalArr进行比较,并从那里获取键值,然后替换为replaceArr
替换replaceArr之后的意思是== [0,10,11,9,1]

在高级谢谢..

最佳答案

您可以简单地使用数组原型中的mapfind


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);

08-26 02:38