JavaScript function is not working. indexOf is undefined - javascript

function filteredArray(arr, elem) { let newArr = [];
Loops through every element of the nested array.
for (let i=0;i<arr.length;i++){
for (let j=0;j<arr[i].length;j++){
If the value on the iteration is equal to the argument passed, it is supposed to set a variable x to be equal to the value of the nested array during the ongoing iteration
if (arr[i][j]==elem){
let x = indexOf(arr[i][j]);
It is supposed to remove the element with index equal to the variable x.
arr[i][j].splice(x,1);
Then it is supposed to push the remained of the nested array to the new array and then subsequently return the new array.
newArr[i].push(...arr[i][j]);
}
}
}
console.log(newArr);
return newArr;
}
HOWEVER THERE IS AN ERROR THAT SAYS 'indexOf is not defined'
I don't understand why it doesn't work. It return indexOf as undefined for every iteration. Please take a look at comments.
Please share your opinion on my code if you don't mind.

indexOf is an array/string method and can be called on an array like array.indexOf(element). In your case your you need to pass the array.
Also you may skip the indexOf because here variable i and j will give the relevant index of the parent and nested array

Related

Can't sort array passed as argument into function (TypeError, cannot call method sort of undefined)

I'm writing a script for sheets using Google Apps script, and I'm passing an array as an argument into a function, and I'd like to sort the array inside, and return a new array that's also been manipulated a bit after it's been sorted. I took some code from another thread here
The code:
function removeDupes(array) {
var outArray = [];
array.sort(); //sort incoming array according to Unicode
outArray.push(array[0]); //first one auto goes into new array
for(var n in array){ //for each subsequent value:
if(outArray[outArray.length-1]!=array[n]){ //if the latest value in the new array does not equal this one we're considering, add this new one. Since the sort() method ensures all duplicates will be adjacent. V important! or else would only test if latestly added value equals it.
outArray.push(array[n]); //add this value to the array. else, continue.
}
}
return outArray;
}
array.sort(); returns the error
"TypeError: Cannot call method "sort" of undefined. (line 91, file
"Code")"
. How can I perform the sort() method on the array passed into this function as an argument?
The error is very clear, whatever code you are using to call removeDupes isn't passing an actual array otherwise the .sort() method would work on the incoming argument. [Side note: why do you need to sort the array if you are interested in removing duplicates anyway?]
Beyond that, the Array.filter() method can filter out duplicates without you having to do all the looping. And, even when you do loop an array, you should not use for/in loops, as they are for enumerating objects with string key names, not arrays. Array looping should be done with regular counting loops, .forEach() or one of the many customized Array.prototype looping methods.
var testArray = ["apple", "bannana", "orange", "apple", "orange"];
function removeDupes(arr){
// The .filter method iterates all of the array items and invokes a
// callback function on each iteration. The callback function itself
// is automatically passed a reference to the current array item being
// enumerated, the index of that item in the array and a reference to
// the array being iterated. A new array is returned from filter that
// contains whatever the callback function returns.
return arr.filter( function( item, index, inputArray ) {
// If the index of the item currently being enumerated is the same
// as the index of the first occurence of the item in the array, return
// the item. If not, don't.
return inputArray.indexOf(item) == index;
});
}
var filteredArray = removeDupes(testArray);
console.log(filteredArray);

Javascript chunky monkey algorithm code break down?

I need a help on breaking how does this code works?
function chunkArrayInGroups(arr, size) {
var newArr = [];
while (arr.length) {
newArr.push(arr.splice(0,size));
}
return newArr;
}
chunkArrayInGroups(["a","b","c","d"],2);
I dont understand how while(arr.length) gets stop and what this whole code is doing. Please break this down.
Array.splice modifies the original Array. When it is called inside while loop, It keeps shortening the original array and after a few iterations, arr.length becomes 0 and evaluates to false.
arr.splice(0,size)
Array.splice will remove elements from index 0 up till size and return them as a new array.
newArr.push(arr.splice(0,size));
The returned array is then pushed to newArr.
while (arr.length)
Array.splice will also mutate the original array so the arr will eventually becomes empty and the loop will end.
while (arr.length) executes until the array is empty, because when it's empty, arr.length equals 0 and it's treated as false.
while array count is "available" push into newArr (and also remove the certain item of it from the root array) when all is done, return it

How to manipulate a JavaScript array passed to a function without changing the original argument array?

I would like to work with an array passed to a function while leaving the original array untouched. Here is a simple example of the problem.
function whyDoesArrChange(arr) {
var newArr = arr;
newArr.push(4);
return arr;
}
console.log(whyDoesArrChange([1,2,3]));
// OUT: [1,2,3,4]
I would like only newArr to be changed, but the arr (from the arguments) returns the pushed value as well (returns [1,2,3,4]). How can I avoid this?
When passing an array to a function, the value of that array is a reference, you have to clone it to break the reference.
There are multiple ways to achieve this:
1 - Using .slice();
var newArr = arr.slice();
2 - Using .concat
var newArr = arr.concat([]);
3 - Using JSON.parse & JSON.stringify
var newArr = JSON.parse(JSON.stringify(arr));
You can check more ways and see how they perform in this jsperf I found.
While Marcos' answer is correct, no doubt. There are more pure Array functions that can be used (A pure function is a function that doesn't alter data outside of its' scope).
Usually, if you'd like to do multiple actions on the array, I would go with Marcos' answer and then do those changes as usual. But when that's not the case, the following information may be useful:
Adding: arr.concat([1]);
Subarray (i to j): arr.slice(i, j + 1);
Removing (i to j): arr.slice(0, i).concat(arr.slice(j + 1));
Also, filter and map are pure function that will not alter the array.
In JavaScript, when you use (=) to assign a value from a variable to another one, you're just passing the entire variable, so each time one or another changes, the other will change too.
According to your question, the best way that works for me is using the native .slice() JavaScript method to the arrays object. For your code:
function whyDoesArrChange(arr) {
var newArr = arr.slice();
newArr.push(4);
return arr;
}
Because reference types (arrays and objects) can get modified inside functions when passed as arguments, while primitive types (numbers, strings, booleans etc.) don't.
You can make a shallow copy the array with .slice() and work on that array, or return a new array with the .concat method.
function whyDoesArrChange(arr) {
return arr.concat(4);
}

Javascript two dimensional arrays [duplicate]

This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 8 years ago.
I have declared a two-dimensional array, like so:
a = [[]]
However, when I try to give a second dimension value using a first dimension index other than 0, it doesn't work:
a[1][0] = "foo" //returns error
Is there a better way around this than manually defining every index you need as an array, i.e.:
a[1] = [];
a[2] = [];
a[3] = [];
//et cetera
N-Dimensional arrays do not exist in javascript - you have to just make arrays containing arrays as elements.
You're getting an error because a = [[]]; declares an array with one element, which happens to also be an array. Therefore a[0] is the internal array, but a[1] does not exist because you never declared it. The easiest way to properly declare a "two dimensional array" would be to use a loop:
var outerArray = [];
var numInternalArrays = 5;
for (var i = 0; i < numInternalArrays; i++) {
outerArray[i] = [];
}
If you know how many elements the root array should have you could do something like this:
var arr =
(Math.pow(2,10)-1).toString(2) // Binary string of 1s. Its length being 10
.split('') // Create an array from this string
.map(function(){return [];}); // Map a new empty array to each index
console.log(arr); // [[],[],[],[],[],[],[],[],[],[]]
This accomplishes the same thing:
for(var arr = [], i=10; i--; arr[i]=[]);
No need to declare arr outside of the for-loop since javascript doesn't have block scope, it will be added to the scope in which it is executed.
a = [[]]
This is an Array, with the first item being an array. Which is why indexing into the first item still works (a[0][0]).
If you want to access the second item as an array, you need to create your array as
a = [[],[]]
See this question for examples of
How can I create a two dimensional array in JavaScript?
If I understand correctly, use a loop:
for (var i = y; i--; a[i] = []);
There are no multidimensional arrays in javascript.
What you are doing is an array of arrays, but the outermost array has only one element (i.e. element 0) whose value is another array. So a[1] (or more generally a[1][x]) is invalid since the outermost array has only one element.
So you can do a[0][x] = "foo" but not the other way around.
So you can either initialize the array with a for loop or do something like var a =[[][][][][]];
You can have the array of arrays start as in:
var a = []; // start with the column array
Then when you want to put something in location [i][j] we can call 'i' the row-index and 'j' the column-index.
if (!a[i]) { // check for row existing
a[i] = []; // .. and create it if not
}
a[i][j] = 'foo'; // put something in the array cell
Note that this only works because we are always putting something in the new row array right after we create it. It might not work if you put 0 or "" in there instead of 'foo'.
There are a lot of things in javascript that are 'false' including 'null' and 'undefined' and '0' and I just don't know if an empty array or an array with one element that is an empty string are considered false. So you would have to do some experimenting with how, exactly to detect a missing row array so you can add it in.

Iterating over two arrays at a time in Javascript

I want to iterate over two arrays at the same time, as the values for any given index i in array A corresponds to the value in array B.
I am currently using this code, and getting undefined when I call alert(queryPredicates[i]) or alert(queryObjects[i]).
I know my array is populated as I print out the array prior to calling this.
//queryPredicates[] and queryObjects[] are defined above as global vars - not in a particular function, and I have checked that they contain the correct information.
function getObjectCount(){
var variables = queryPredicates.length; //the number of variables is found by the length of the arrays - they should both be of the same length
var queryString="count="+variables;
for(var i=1; i<=variables;i++){
alert(queryPredicates[i]);
alert(queryObjects[i]);
}
The value of the length property of any array, is the actual number of elements (more exactly, the greatest existing index plus one).
If you try to access this index, it will be always undefined because it is outside of the bounds of the array (this happens in the last iteration of your loop, because the i<=variables condition).
In JavaScript the indexes are handled from 0 to length - 1.
Aside of that make sure that your two arrays have the same number of elements.
If queryPredicates does not have numerical indexes, like 0, 1, 2, etc.. then trying to alert the value queryPredicates[0] when the first item has an index of queryPredicates['some_index'] won't alert anything.
Try using a for loop instead:
stuff['my_index'] = "some_value";
for (var i in stuff)
{
// will alert "my_index"
alert(i);
// will alert "some_value"
alert(stuff[i]);
}
Arrays in JS are zero based. Length is the actual count. Your loop is going outside the bounds of the array.

Categories