Uncaught TypeError Cannot read property indexOf of undefined

0 votes

In this code I want to remove an element from the cart_products array.

var cart_products = ["17^1", "19^1", "18^1"];
var product = 17;

$.each(cart_products,function(key, item) {
    if(item.indexOf(product+"^") !== -1){
        cart_products.splice(key, 1);
    }
});

But I get this error in Google Chrome console:

Uncaught TypeError: Cannot read property 'indexOf' of undefined

Is there something wrong with the code?

Thanks for your help.

Jun 2, 2020 in Java-Script by kartik
• 37,490 points
3,939 views

1 answer to this question.

0 votes

Hello @kartik,

First of all, you don't need to use a jQuery each for this.

Secondly, it's not a great idea to alter an array that you are operating on. 

If you're trying to remove elements from an array, use Filter.

Filter has the following signature:

someArray.filter(function(item, index, array) {
  // return a value that is truthy to keep an item or falsey to remove it
})

Filter returns a new array with only the values that match what you want. That means you don't mess with your original array, which is a good idea.

In your case it would look like this:

var filteredProducst = cart_products.filter(function(item) {
  return item.indexOf(product + "^")
})

Hope this help!!

answered Jun 2, 2020 by Niroj
• 82,680 points

Related Questions In Java-Script

0 votes
1 answer

How to Scroll to the top of the page using JavaScript?

Hii @kartik, Using javascript <script> $("a[href='#top']").click(function() { ...READ MORE

answered Apr 2, 2020 in Java-Script by Niroj
• 82,680 points
139 views
0 votes
1 answer

Error: Use of const in strict mode

Hello, If this is happening in nodejs, it ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,680 points
1,081 views
0 votes
1 answer

Cannot install NodeJs: /usr/bin/env: node: No such file or directory

Hii, If you already have nodejs installed (check ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,680 points
2,309 views
0 votes
1 answer

How to get the directory of the currently running file?

Hello @kartik, This should do it: import ( ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,680 points
206 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,680 points
329 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,680 points
1,672 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,680 points
409 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,680 points
167 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'setState' of undefined

Hello kartik, You dont have to bind anything, ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,680 points
185 views
0 votes
1 answer

Uncaught TypeError: Cannot use 'in' operator to search for '324'

Hello @kartik, You have a JSON string, not ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,680 points
3,086 views