The examples in this article summarize common operating techniques for JavaScript arrays. Share it with everyone for your reference. The details are as follows:
Foreword
I believe everyone is used to common array-related operations in jquery or underscore and other libraries, such as $.isArray, _.some, _.find and other methods. This is nothing more than some additional packaging for array operations in native js.
Here we mainly summarize the commonly used APIs for JavaScript array operations. I believe it will be helpful for everyone to solve program problems.
1. Properties
An array in JavaScript is a special object. The index used to represent the offset is a property of the object, and the index may be an integer. However, these numeric indices are converted to string types internally because property names in JavaScript objects must be strings.
2. Operation
1 Determine array type
2 Arrays and Strings
Very simple: to convert from array to string, use join; to convert from string to array, use split.
3 Find elements
I believe that everyone commonly uses the string type indexOf, but few know that the indexOf of an array can also be used to find elements.
//
var objInArray = [
{
name: 'king',
Pass: '123'
},
{
name: 'king1',
Pass: '234'
}
];
console.log(objInArray.indexOf({
name: 'king',
Pass: '123'
})); // -1
var elementOfArray = objInArray[0];
console.log(objInArray.indexOf(elementOfArray)); // 0
As can be seen from the above, for an array containing objects, the indexOf method does not obtain the corresponding search result through in-depth comparison, but only compares the references of the corresponding elements.
4 Array connection
Use concat. Please note that a new array will be generated after using concat.
For adding elements, you can use push and unshift respectively, and for removing elements, you can use pop and shift respectively.
//Add to the end of the array
array.push(6);
console.log(array); // [2, 3, 4, 5, 6]
//Add to the head of the array
array.unshift(1);
console.log(array); // [1, 2, 3, 4, 5, 6]
//Remove the last element
var elementOfPop = array.pop();
console.log(elementOfPop); // 6
console.log(array); // [1, 2, 3, 4, 5]
//Remove the first element
var elementOfShift = array.shift();
console.log(elementOfShift); // 1
console.log(array); // [2, 3, 4, 5]
6 splice methods
Main two uses:
① Add and delete elements from the middle of the array
② Obtain a new array from the original array
Of course, the two uses are combined in one go. Some scenes focus on the first use, and some focus on the second use.
Add and delete elements from the middle of the array. The splice method adds elements to the array. The following parameters need to be provided
① Starting index (that is, where you want to start adding elements)
② The number of elements to be deleted or the number of elements to be extracted (this parameter is set to 0 when adding elements)
③ Elements you want to add to the array
7 Sort
Mainly introduce two methods: reverse and sort. Array reversal uses reverse, and the sort method can be used not only for simple sorting, but also for complex sorting.
var objInArray = [
{
name: 'king',
Pass: '123',
index: 2
},
{
name: 'king1',
Pass: '234',
index: 1
}
];
// Sort the object elements in the array in ascending order according to index
var compare = function(o1, o2) {
Return o1.index > o2.index;
};
objInArray.sort(compare);
console.log(objInArray[0].index < objInArray[1].index); // true
8 Iterator methods
Mainly includes forEach and every, some and map, filter
I believe everyone knows forEach, and I will mainly introduce the other four methods.
The every method accepts a function that returns a Boolean value and applies the function to each element in the array. This method returns true if the function returns true for all elements.
Some methods also accept a function whose return value is a Boolean type. As long as there is an element that causes the function to return true, the method returns true.
var isEven = function(num) {
Return num % 2 === 0;
};
var nums1 = [1, 2, 3, 4];
console.log(nums1.some(isEven)); // true
Both methods map and filter can generate new arrays. The new array returned by map is the result of applying a function to the original elements. Such as:
3. Summary
There is also the problem that some of the above methods are not supported by low-level browsers, and other methods need to be used for compatible implementation.
These are common methods that may not be easy for everyone to think of. You may wish to pay more attention to it.
I hope this article will be helpful to everyone’s JavaScript programming design.