Remove a particular element from an array in JavaScript

Report
Question

Please briefly explain why you feel this question should be reported .

Report
Cancel

Code:

var array = [2, 3, 9, 6];
console.log(array)
var index = array.indexOf(3);
if (index > -1) {
  array.splice(index, 1);
}
console.log(array);

Output:
[2, 3, 9, 6]
[2, 9, 6]

indexOf() method
Returns the position of the first occurrence of a substring.

Syntax:
strObj. indexOf( subString [, startIndex ])

Parameters:

  • strObj: (Required) A String object or string literal.
  • subString: (Required) The substring to search for in the string
  • startIndex: (Optional) The index at which to begin searching the String object. If omitted, search starts at the beginning of the string.

Note:

  • The indexOf method returns the beginning of the substring in the String object. If the substring is not found, -1 is returned.
  • If startindex is negative, startindex is treated as zero. If it is greater than the highest index, it is treated as the highest index.
  • Searching is performed from left to right. Otherwise, this method is identical to lastIndexOf.
    slice() method

slice() method
Returns the position of the first occurrence of a substring.

Syntax:
stringObj.slice( start , [ end ])

Parameters:

  • stringObj: (Required) A String object or string literal.
  • start: (Required) The index to the beginning of the specified portion of stringObj.
  • end: (Optional) The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end. If this value is not specified, the substring continues to the end of stringObj.
0
C 3 years 0 Answer 510 views 0

About w3r

Leave an answer

Browse

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>