简体   繁体   中英

How to get the index of a multidimensional array and splice that array

There is a problem in FreeCodeCamp.I'm not here to search solution to that problem. While trying to solve the problem I found some lines of my code do not work. I can not understand why that is not working. So I am here to ask YOU, good people, to help me.

problem

There is a function. I will pass an array and a number to that function. And what I need to return is also an array. The array is a multidimensional array.

what I want to do

First of all I want to check if the inner or subarray contains the number i passed while calling the function. If that contains i need the index of that number in that subarray. Then I want to delete the number from that subarray using splice(). At last I wanted to return an array where there are sub arrays in it but none of them contain the given number.

where i am stuck in

But I am stuck in finding the index of the number in sub arrays, how can i use splice() to delete the number? Is it possible to do this way? Do you have any better suggestion for me?

my code

where for the first for loop it just prints -1 for the second loop, it prints the index of the array,not the index of the subarray.

 function filteredArray(arr, elem) { let newArr = []; // Only change code below this line newArr = [...arr] let L = arr.length; for (let i = 0; i < L; i++) { // -------1----------- for (elem in newArr[i]) { console.log(newArr[i].indexOf(elem)); } } console.log('first loop ends') for (let i = 0; i < L; i++) { // --------2--------- for (let j = 0; j < newArr[i].length; j++) { if (newArr[i][j] == elem) { console.log(newArr[i].indexOf(elem)) } } // Only change code above this line return newArr; } } console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

 function filteredArray(arr, elem) { let newArr = []; // Only change code below this line newArr = [...arr] let L = arr.length; // for (let i = 0; i < L; i++) { // // -------1----------- // for (elem in newArr[i]) { // console.log(newArr[i].indexOf(elem)); // } // } console.log('first loop ends') for (let i = 0; i < L; i++) { // --------2--------- for (let j = 0; j < newArr[i].length; j++) { if (newArr[i][j] == elem) { console.log(newArr[i].indexOf(elem)) } } // Only change code above this line return newArr; } } console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

When removing values from an array you don't want to use a mutating method like .splice() -- the original array will change. If .splice() removes a number, the length of the array decreases and all indexes at and after the index of the removed number will shift (unless you replace that number instead). The non-mutating methods such as filter() and .map() makes a copy of the array and returns the copy leaving the original array intact. See this article for a easy reference of what mutates and what doesn't.

You could simplify the process of removing a given number from an array of arrays by using .map() on each sub-array and .filter() each sub-array with the condition of returning only numbers that do not equal the given number.

 const data = [[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]]; const filterCols = (target, arrArr) => arrArr.map(sub => sub.filter(num => num;== target)). console,log(filterCols(3; data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM