Filter in uniapp to obtain the data of id: 1 in an object in the array

// 假设studentData是包含多个学生信息的数组
const studentData = [
  {id: 1, name: '小明', age: 18},
  {id: 2, name: '小红', age: 20},
  {id: 3, name: '小刚', age: 19},
  {id: 4, name: '小李', age: 22},
];
// 过滤获取id为1的学生信息
const result = studentData.filter(item => item.id === 1);
// 输出结果
console.log(result);
// 输出: [{id: 1, name: '小明', age: 18}]

The Array.filter() method in JavaScript is used to filter and obtain the data of id:1 in an object in the array

extend:

filter()

Create a new array, the elements of the new array are elements that meet the specified conditions, will not detect empty arrays, and will not change the original array.

map()

Returns a new array, the elements of the new array are the values ​​of the elements of the original array processed in order.

foreach()

Loop, call each element of the array, passed to the callback function, can not end the loop.
Example: Looping the data of an array into a new array

concat()

Merge arrays. Merge arrays and arguments, Merge arrays and arrays

join()

Put all the elements in the array into a string.
Elements are separated by the specified delimiter.

push()

Add elements at the end of the array, you can add multiple elements at a time

pop()

delete an element at the end of the array

shift()

delete the first element of the array

unshift()

Add elements at the beginning of the array, you can add multiple elements at a time

splice()

Delete, insert, replace elements.
Syntax: this.arr.splice(index, num, value)

sort()

sort the array

reverse()

invert the array

slice()

Return the specified element from the array
Syntax: arr.slice(start,end), that is: the start and end position of the element to be returned.
start is required. Specifies where to start selection. If it is a negative number, then the position is calculated from the end of the array. -1 refers to the last element.

end is optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array slice. If this parameter is not specified, the split array contains all elements from start to end of the array. If this parameter is negative, it specifies the elements counting from the end of the array.

find()

Returns the first array element that meets the requirements.

findIndex()

Returns the subscript of the array element that meets the function condition, and each element in the array calls the function execution once: the empty array will not be executed.
Returns the position of the first element in the array that meets the condition, and returns -1 if there is no element that meets the condition.

some()

Check whether the elements in the array meet the specified conditions, and execute each element of the array in turn:
if one element meets the conditions, the expression returns true, and the remaining elements will not be tested.

Returns false if there is no element that satisfies the condition.

every()

Use the specified function to check whether all elements in the array meet the condition, and empty arrays will not be executed.

If an element in the array is detected to be unsatisfactory, the entire expression returns false, and the remaining elements are not checked.

Returns true if all elements satisfy the condition.

Guess you like

Origin blog.csdn.net/qq_37194189/article/details/132082369