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,520 points
19,549 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,840 points

Related Questions In Java-Script

0 votes
0 answers

Uncaught TypeError: Cannot read property 'top' of undefined

I have two different kinds of sticky ...READ MORE

May 11, 2022 in Java-Script by Kichu
• 19,040 points
3,383 views
0 votes
0 answers

Cannot read property 'push' of undefined when combining arrays

When pushing an array's contents to another ...READ MORE

Nov 16, 2022 in Java-Script by Ashwini
• 5,430 points
569 views
0 votes
0 answers

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

I was trying to change a string ...READ MORE

May 9, 2022 in Java-Script by Kichu
• 19,040 points
6,852 views
0 votes
0 answers

Sort array of objects by string property value

I have this array of JavaScript objects: var objs ...READ MORE

May 10, 2022 in Java-Script by Kichu
• 19,040 points
924 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,840 points
3,188 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,840 points
18,627 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,840 points
3,261 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,840 points
1,295 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,840 points
832 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,840 points
8,550 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP