大家好,我对javascript
来说还很陌生,所以我寻求您的帮助。
我的问题如下:
如果有类似数组
var arr1 = ["a", "b", "a" "c"];
我想找到每个“ a”并将其替换为另一个数组的某个元素
var arr2 = [1, 2, 3, 4, 5, 6,];
现在如何用arr2的第一个元素替换每个“ a”? 每个“ b”都有第二个,依此类推。
这有可能吗?保持位置吗?
答案 0 :(得分:3)
您可以使用一个对象将每个字符与特定的整数关联:obj
:
const obj = {a:1, b:2, c:3, d:4, e:5, f:6};
然后使用.map
方法与辅助对象arr1
重新映射数组obj
:
arr1.map(e => obj[e])
.map
方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。
const arr1 = ["a", "b", "a", "c"];
const obj = {a:1, b:2, c:3, d:4, e:5, f:6};
console.log(
arr1.map(e => obj[e])
)
这样,您可以通过更改obj
来更改字符和数字之间的关系。您甚至可以将变量设置为obj
的值,例如:
const arr1 = ["a", "b", "a", "c"];
const otherLetter = "z";
const obj = {a:otherLetter, b:2, c:3, d:4, e:5, f:6};
console.log(
arr1.map(e => obj[e])
)
答案 1 :(得分:1)
您可以采用动态方法并建立成功的哈希表并采用该值。
var array1 = ["a", "b", "a", "c"],
array2 = [1, 2, 3, 4, 5, 6],
hash = Object.create(null),
index = 0;
result = array1.map(v => v in hash ? hash[v] : hash[v] = array2[index++]);
console.log(result);
没有index
但带有array2
变异的版本
var array1 = ["a", "b", "a", "c"],
array2 = [1, 2, 3, 4, 5, 6],
hash = Object.create(null),
result = array1.map(v => v in hash ? hash[v] : hash[v] = array2.shift());
console.log(result);
答案 2 :(得分:0)
这可能会帮助您:
var arr1 = ["a", "b", "a", "c", "f"];
var arr2 = [1, 2, 3, 4];
//print out arrays
console.log("arr1: ", arr1);
console.log("arr2: ", arr2);
//loop through the arr1
var i;
//loop until you reach the length of array1.
for (i = 0; i < arr1.length; i++) {
//check type of item in arr1
switch (arr1[i]) {
case "a":
arr1[i] = arr2[0];
break;
case "b":
arr1[i] = arr2[1];
break;
case "c":
arr1[i] = arr2[2];
break;
default:
//default if none of the above cases match with entry in array1
arr1[i] = -1;
}
}
//print out arrays
console.log("arr1: ", arr1);
console.log("arr2: ", arr2);
答案 3 :(得分:0)
您可以使用parseInt
从arr1
中的字符生成索引,如下所示:
let indices = arr1.map(str => parseInt(str, 36) - 10);
parseInt
的第二个参数是基数,36允许使用0-9
和a-z
指定数字。因为a = 10
我们需要减去10才能获得正确的索引(a
->索引0
)。
然后使用这些索引从arr2
中获取相应的元素:
let result = indices.map(index => arr1[index]);
一个简短的示例:
let arr1 = ['a', 'b', 'a', 'c'],
arr2 = [1, 2, 3, 4, 5, 6],
result = arr1.map(s => parseInt(s, 36) - 10).map(i => arr2[i]);
console.log(result);
答案 4 :(得分:0)
您可以创建arr1
的字符串表示形式并替换字符:
var arr1 = ["a", "b", "a", "c"];
var arr2 = [1, 2, 3, 4, 5, 6,];
var str = arr1.join(',');
arr2.forEach((number, index)=>{
var character = arr1[index];
if(character && isNaN(arr1[index])){
var re = new RegExp(character,"g");
str = str.replace(re, number);
}
});
arr1 = str.split(',');
console.log(arr1);
答案 5 :(得分:0)
这是我(简单得多)的答案:
var arr1 = ["a", "b", "a", "c"];
//I'd like to find and replace each "a"
//with a certain element of an other array
var arr2 = [1, 2, 3, 4, 5, 6,];
for(var i in arr1) {
arr1[i] = arr2[arr1[i].charCodeAt(0) - 97];
}
console.log('arr1 is now: [' + arr1 + ']');
答案 6 :(得分:0)
var arr1 = ["a", "b", "a", "c"];
var arr2 = [1, 2, 3, 4, 5, 6];
var uniqueItems = Array.from(new Set(arr1));
for(i=0;i<uniqueItems.length;i++){
arr1.forEach(function(item, j) { if (item == uniqueItems[i]) arr1[j] = arr2[i]; });
}
console.log(arr1);