Javascript 如何合并数组,使两个数组中原始元素的索引保持不变
Javascript how to merge arrays so that the indexes of original elements in both arrays stays the same
如何在JS中合并数组,使两个数组中原始元素的索引保持不变?
似乎传播数组没有满足我的需要:
let testArray: Array<any> = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
console.log(testArray);
let testar = [...testArray, ...otherTestArray];
console.log(testar);
2:"test2"
4:"test4"
15:"test15"
19:"test3"
21:"test5"
新数组中元素的问题索引已更改。
那么如何才能高效的解决这个问题呢?
一般来说,稀疏数组是一个非常糟糕的主意,但如果你有这样做,你可以使用Object.assign
:
let testArray = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
const finalArr = Object.assign([], testArray, otherTestArray);
console.log(finalArr);
// (16) [empty × 2, "test2", "test3", "test4", "test5", empty × 9, "test15"]
您可以将 Object.assign
和数组作为目标。
let testArray = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
console.log(testArray);
let testar = Object.assign([], testArray, otherTestArray);
console.log(testar);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以遍历每个忽略未定义的值并将该值分配给同一位置的另一个数组...
let testArray = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
//console.log(testArray);
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
//console.log(otherTestArray);
let result = [];
for (var arr of [testArray, otherTestArray]){
console.log(arr.length);
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== void(0)) // ignore undefined values
result[i] = arr[i];
}
}
console.log(result);
如何在JS中合并数组,使两个数组中原始元素的索引保持不变?
似乎传播数组没有满足我的需要:
let testArray: Array<any> = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
console.log(testArray);
let testar = [...testArray, ...otherTestArray];
console.log(testar);
2:"test2"
4:"test4"
15:"test15"
19:"test3"
21:"test5"
新数组中元素的问题索引已更改。
那么如何才能高效的解决这个问题呢?
一般来说,稀疏数组是一个非常糟糕的主意,但如果你有这样做,你可以使用Object.assign
:
let testArray = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
const finalArr = Object.assign([], testArray, otherTestArray);
console.log(finalArr);
// (16) [empty × 2, "test2", "test3", "test4", "test5", empty × 9, "test15"]
您可以将 Object.assign
和数组作为目标。
let testArray = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
console.log(testArray);
let testar = Object.assign([], testArray, otherTestArray);
console.log(testar);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以遍历每个忽略未定义的值并将该值分配给同一位置的另一个数组...
let testArray = [];
testArray[4] = 'test4';
testArray[2] = 'test2';
testArray[15] = 'test15';
//console.log(testArray);
let otherTestArray = [];
otherTestArray[3] = 'test3';
otherTestArray[5] = 'test5';
//console.log(otherTestArray);
let result = [];
for (var arr of [testArray, otherTestArray]){
console.log(arr.length);
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== void(0)) // ignore undefined values
result[i] = arr[i];
}
}
console.log(result);