Created
September 20, 2020 11:27
-
-
Save uiyuvi/abab2b6405e5ddadd98587a8cc349bde to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array1 = [ | |
[10, 25, 30, 55, 1], | |
[3, 15, 67, 78, 2], | |
[45, 67, 55, 65, 4] | |
]; | |
var array2 = [ | |
[10, 25, 30, 55, 2], | |
[3, 15, 67, 78, 3], | |
[45, 67, 55, 65, 4], | |
[61, 76, 87, 56, 5] | |
]; | |
console.log(array1); | |
console.log(array2); | |
function getArrayWhenLastElementMatches(targetArray, value) { | |
var matchedElements = targetArray.find(function (array) { | |
return array[array.length - 1] === value; | |
}); | |
matchedElements = matchedElements | |
? matchedElements | |
: Array(targetArray[0].length).fill(null); | |
return matchedElements; | |
} | |
var array3 = array1.concat(array2); | |
var array3LastIndexElements = array3.map((array) => array[array.length - 1]); | |
var newArraySize = Math.max(...array3LastIndexElements); | |
var newArray = []; | |
for (let arrayIndex = 1; arrayIndex <= newArraySize; arrayIndex++) { | |
var arrayWhenLastElementMatchesInArray1 = getArrayWhenLastElementMatches( | |
array1, | |
arrayIndex | |
); | |
var arrayWhenLastElementMatchesInArray2 = getArrayWhenLastElementMatches( | |
array2, | |
arrayIndex | |
); | |
newArray.push( | |
arrayWhenLastElementMatchesInArray1.concat( | |
arrayWhenLastElementMatchesInArray2 | |
) | |
); | |
} | |
console.log(newArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment