You have an original array and another array where the elements are grouped in threes. You want to find the group in the second array that corresponds to a given element from the original array.
Source code viewer
const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const groupedArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; // Assuming you want to find the group for the element 4 from the original array const elementToFind = 4; // Find the index of the element in the original array const elementIndex = originalArray.indexOf(elementToFind); // Calculate the corresponding group index const groupIndex = Math.floor(elementIndex / 3); // Get the corresponding group from the grouped array const correspondingGroup = groupedArray[groupIndex]; console.log("Original Array:", originalArray); console.log("Grouped Array:", groupedArray); console.log(`Corresponding Group for ${elementToFind}:`, correspondingGroup);Programming Language: ECMAScript