JavaScriptは、ある条件に合致する行列の値を検索し、条件に該当する値を新しい配列に保存します.

811 ワード

1.find関数とes 6矢印関数を利用して、配列の中で条件を満たす最初の要素を取ります.
var newArray  = {};//   
var array = [
 {name:'aiai',age:18},
 {name:'sisi',age:18},
 {name:'lulu',age:18},
 {name:'sisi',age:19}
]; 
//             
newArrar = this.array.find((item) => {
   if(item.name === 'sisi'){
     return item
   }
 })
//   newArray = {name:'sisi',age:18}
2.条件に合ったすべての要素を検索します.
var newArray  = [];//   
var j = 0;
var array = [
 {name:'aiai',age:18},
 {name:'sisi',age:18},
 {name:'lulu',age:18},
 {name:'sisi',age:19}
]; //    

//             
for(let i in array){
 if(array[i].name == 'sisi'){
  newArray[j++] = array[i]
 }
}
//   newArray = [{name:'sisi',age:18},{name:'sisi',age:19}]