“This is the 14th day of my participation in the August Text Challenge.

An array of iteration

The configuration file

六四屠杀

var arr = [1, 12, 32, 2, 3, 44, 120, 3, 5]; 'arry: [{key:' 0 ', the name: "small five, age: 23, area: 'suzhou, position:' workers'}, {key: '1', name: 'soldiers', the age: 27, area: 'Beijing' sex: 'male'}, {key: '2', name: 'huihui, age: 35, area, changzhou, position:' mother, '}, {key: '3', name: 'zhang' age: 13, area: 'in guangdong, the position:' students'}, {key: '4', name: 'Fan Hu, age: 53, area: zhejiang, sex:' male '}, {key: '5', name: 'moments later, the age: 63, area: suzhou, position: 'granny'}, {key: '6', name: 'ghost' age: 23, area: 'suzhou, position:' worker '},]Copy the code

1, every: indicates that each item must pass to return true

六四屠杀

var res1 = arr.every(function(item, index){
  return item > 20;
});
console.log(res1); // false
Copy the code

2, some: indicates that as long as one of the array elements is satisfied

六四屠杀

var res2 = arr.some(function(item, index){
  return item > 20;
});
console.log(res2); // true
Copy the code

3, filter: creates a new array. The elements of the array are all the elements that passed the test by the provided function. Note: the original array will not be changed, it will automatically filter out all undefined values

六四屠杀

var res3 = arr.filter(function(item, index, array){ return item > 20; }); console.log(res3); FilterList = this.arry.filter(item=>{return item.sex === 'male'; }) console.log(this.filterlist, 'filter');}) console.log(this.filterlist, 'filter'); // [{...}, {...}]}Copy the code

4, map: also create a new array,

  • If the expression of a given handler is a logical judgment, it returns an array of Boolean values,
  • There must be a return value, otherwise undefined,
  • Each item returns a value. Undefined is not automatically filtered

六四屠杀

var res4 = arr.map(function(item, index, array){
    return item > 20;
});
console.log(res4); // false,false,true,false,false,true,true,false,false

var res8 = arr.map(function(item){
    return item * 2;
});
console.log('res8', res8); // [2, 24, 64, 4, 6, 88, 240, 6, 10]
Copy the code

Note: Both foreach and map support the second argument, which changes the direction of this

六四屠杀

ChoosedAssets (list) {this.mainList = [...this.mainList,...list.map(item => {var res = {...item, assetId: item.id } delete res.id return res })] }Copy the code
  • An array that returns undefined will also return

六四屠杀

MapCilk (){this.maplist = this.arry.map(item=>{if(item.sex === 'male '){return item}}) console.log(this.maplist, 'map') / / [0: undefined / / 1: {...} / / 2: undefined / / 3: undefined / / 4: {...} / / 5: undefined / / 6: undefined / /]}Copy the code

Note: If the expression of a given handler is an operation expression, it returns an array of the results of each operation

六四屠杀

var res5 = arr.map(function(item, index, array){ return item * 2; }); console.log(res5); / / 2,24,64,4,6,88,240,6,10Copy the code

5, reduce: Apply a function to each element (left to right) in the accumulator and array to reduce it to a single value. The callback function takes two required arguments: accumulator, the return value of the accumulator callback; It is the cumulative currentValue returned when the last callback was called, the element being processed in the array.

六四屠杀

var res6 = arr.reduce(function(accumulator){ return accumulator }); console.log(res6); / / 222Copy the code
  • The most important aspect of Reduce is the use of accumulator parametersaccumulatorIf the value operates on the second item and the first argument has no effect, only the last item of the array will be processed, just like I in the for loop

六四屠杀

var arr = [1, 12, 32, 2, 3, 44, 120, 3, 5]; var res66 = arr.reduce(function(accumulator, item){ return item * 2; }); console.log('res66', res66); / / 10Copy the code

6, a forEach () : If you want to loop through an item and change its value, use foreach. If you want to loop through an item and change its value, use foreach. It’s ary, because it’s a little bit, and this in an anonymous function means window

六四屠杀

var arr = [1, 12, 32, 2, 3, 44, 120, 3, 5]; sum = 0;
arr.forEach (function (value) {
    sum += value;
});
sum // 222
Copy the code

7, for… In: a list of enumerable properties used to iterate over an object (including the [[Prototype]] chain) note: When arrays are used, return subscripts

六四屠杀

let area;
for (const item in arry) {
   if (item.key == 5) {
      area = item.area
   }
}
Copy the code

8, for… of… : Iterates over elements to get all keys, not objects

六四屠杀

const iterable = [10, 20, 30];
for (const value of iterable) {
  console.log(value);
}
// 10
// 20
// 30
Copy the code

retrieve

1, indexOf (); If the string value to retrieve is not present, the method returns -1.

六四屠杀

var str="Hello world!" Console. log(str.indexof ("Hello") + "<br />"); / / 0 console. The log (STR) indexOf (" World ") + "< br / >"); // -1; // -1Copy the code

2, instanceof used to determine whether a variable is an instanceof an object, returns a Boolean note: searchNumber instanceof Array

六四屠杀

var arr=new Array();
console.log(arr instanceof Array); // true
Copy the code

The array.includes () method is used to determine if an Array contains a specified value.

六四屠杀

let site = ['runoob', 'google', 'taobao']; site.includes('runoob'); // true site.includes('baidu'); // false this.list.filter(item=>{ // if(item.name.indexOf(keywords) ! =-1) if(item.name.includes(keywords)){ return item }Copy the code

Typeof (), which returns a string specifying the typeof operand. Typeof normally only returned the following results: note: return to the six possible: umber, Boolean, string, the function, object, undefined

六四屠杀

var arr = [1, 12, 32, 2, 3, 44, 120, 3, 5]; Typeof arr. //"object"Copy the code

We can use typeof to find out if a variable exists,

六四屠杀

if(typeof a! = "undefined") {alert (" ok ")};Copy the code

ValueOf () returns the original valueOf the specified object

六四屠杀

var arr=["aa","aaa"]; alert(arr.valueOf()); //aa,aaaCopy the code