How a large array of JavaScript can be quickly found and deleted based on the key of the object

Source: Internet
Author: User

Find: On the code.

functionIsbigenough (Element) {returnElement >= 15;}varRet1 = [12, 5, 8, 130, 44].findindex (Isbigenough); Console.log (RET1);//Index of 4th element in the Array is returned,//So this would result in ' 3 'varObjarr = [{id:1, Name: ' Jiankian '}, {id:23, Name: ' Anan '}, {id:188, Name: ' Superme '}, {id:233, Name: ' Jobs '}, {id:288, Name: ' Bill ', age:89}, {id:333}] ;varRet2 = Objarr.findindex ((v) = = {    returnV.id = = 233;}); Console.log (Ret2);//return 3

findIndex()method returns the index of the first element in the array that satisfies the provided test function. otherwise returns-1.

Grammar:
arr.findIndex(callbackthisArg])
Parameters:

callbackFor each element in the array, the callback function is executed, and the following three parameters are automatically passed in execution:

element
The current element.
index
The index of the current element.
array
findIndex an array of calls.

thisArgOptional. callback this the value to use as the object at execution time.

Describe:

findIndexThe method executes the function once for each array index 0..length-1 (including) in the arrays callback until a function is found that callback returns true the value of the real value (forced). If such an element is found, the findIndex index of the element is returned immediately. If the callback never returns true, or if the array is length 0, findIndex 1 is returned. Unlike some other array methods, such as Array#some, in a sparse array, the callback function is called even for an index of an entry that does not exist in the array.

The callback function is called with three parameters: the value of the element, the index of the element, and the array being traversed.

If a thisArg parameter is provided findIndex , it will be used as a this function of each callback when it is called. If it is not provided, it will be used undefined .

findIndexThe called array is not modified.

The index range of an element is determined the first time the function is called callback , so a findIndex new element added to the array after the method begins execution will not be accessed by the callback function. If the value of an element in an array that has not yet been accessed by a function is changed by the callback callback function, when the callback function accesses it, its value is the current value that will be accessed based on its index in the array. The deleted element will still be accessed.

Example finds the index of the first prime number element in an array

The following example finds the index of an element of a prime in an array (returns-1 if no prime number exists).

function IsPrime (element, index, array) {  var start = 2;    while (Start <= math.sqrt (Element)) {    if (element% start++ < 1) {      return
    false;    }  }   return element > 1;} Console.log ([//  -1, not found//  2

Delete: on the code.

var myFish = [' Angel ', ' clown ', ' Mandarin ', ' sturgeon '];myfish.splice (//  Insert ' at index 2 ') Drum '//  myFish changed to ["Angel", "clown", "drum", "Mandarin", "sturgeon"]myfish.splice ( // deletes an item (that is, ' drum ') from the location of index 2 // MyFish changed to ["Angel", "Clown", "Mandarin", "sturgeon"]
Grammar
array. Splice ( start ) array . Splice ( start deleteCount array . Splice ( start deleteCount item1 item2 ,...)
Parameters
start?
Specifies the start position of the modification (counted from 0). If the length of the array is exceeded, the content is added from the end of the array, or, in the case of a negative value, the number (from-1) that starts at the bottom of the array, or if only the start parameter is used instead of the DeleteCount, item, such as: array . Splice ( start ), Represents the element that deletes [start,end].
deleteCountoptions available
An
integer that represents the number of array elements to remove. If deleteCount it is 0, the element is not removed. In this case, you should add at least one new element. If deleteCount  it is greater than the start total number of elements that follow, then the elements from the start subsequent will be deleted (with the start bits).
If DeleteCount is omitted, it is equivalent (Arr.length-start).
item1, item2, ...options available
The
element to add into the array, start starting at the position. If not specified, splice()  only the array element is deleted.

The splice method uses the deletecount parameter to control whether to delete or add:
The start parameter is required to indicate the starting position (counting from 0), such as: Start=0 starting from the first, and start>= array.length-1 representing the last start.
①, starting from the start position, deletes the [start,end] element.
Array.splice (Start)
②, starting from the start position, deletes the [Start,count] element.
Array.splice (Start, DeleteCount)
③, starting from start position add item1, item2, ... Elements.
Array.splice (Start, 0, Item1, item2, ...)

return value

An array of elements that are deleted. If only one element is deleted, an array containing only one element is returned. If no element is deleted, an empty array is returned.

Describe

If the number of elements added into an array is not equal to the number of elements being deleted, the length of the array will change accordingly.

Hints and Notes

Note: Notice that the splice () method works differently than the slice () method, and the splice () method modifies the array directly.

Example removes 0 elements from the 2nd bit, inserting "drum"
var myFish = ["Angel", "Clown", "Mandarin", "Surgeon"//var removed = Myfish.splice (2, 0, "drum"// // deleted element array: [], no element deleted

Delete 1 elements starting from 3rd bit
var myFish = [' Angel ', ' clown ', ' drum ', ' Mandarin ', ' sturgeon ']; var removed = Myfish.splice (3, 1); // myfish:["Angel", "clown", "drum", "sturgeon" after Operation // array of deleted elements: ["Mandarin"]

Remove 1 elements from the 2nd bit and insert "trumpet"
var myFish = [' Angel ', ' clown ', ' drum ', ' sturgeon ']; var removed = Myfish.splice (2, 1, "trumpet"// // array of deleted elements: ["drum"] 

Remove 2 elements from the No. 0 bit, then insert "Parrot", "Anemone" and "blue"
var myFish = [' Angel ', ' clown ', ' trumpet ', ' sturgeon ']; var removed = Myfish.splice (0, 2, ' parrot ', ' anemone ', ' Blue '); //  //  deleted elements array: ["Angel", "clown"]

Delete 2 elements starting from 2nd bit
var myFish = [' Parrot ', ' anemone ', ' Blue ', ' trumpet ', ' sturgeon ']; var removed = Myfish.splice (myfish.length-3, 2); //  //  deleted elements array: ["Blue", "trumpet"]

Delete all elements starting from 2nd bit
var myFish = [' Angel ', ' clown ', ' Mandarin ', ' sturgeon ']; var removed = Myfish.splice (2); //  //  deleted elements array: ["Mandarin", "sturgeon"]

How a large array of JavaScript can be quickly found and deleted based on the key of the object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.