Languages

JavaScript - "TypeError: array.splice is not a function"?

0 points
Asked by:
lena
714

How can I fix the following error?

"TypeError: array.splice is not a function"

My code:

// ONLINE-RUNNER:browser;

var array = { 0: 'a', 1: 'b' };

array.splice(0, 1);

I want to remove a 0: 'a' key/value pair from the array variable.

1 answer
0 points
Answered by:
lena
714

You named the variables wrong. What you are trying to do is use an object to create an array.

If you want to remove the object property use delete operator.

// ONLINE-RUNNER:browser;

var object = { 0: 'a', 1: 'b' };

delete object[0];

console.log(JSON.stringify(object)); // {"1":"b"}

 

See also

  1. JavaScript - array delete operator vs splice method

References

  1. delete operator - JavaScript | MDN
  2. Array.prototype.splice() - JavaScript | MDN
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join