var arr = [
{name:"李四",age:18},
{name:"王四",age:20},
{name:"zs",age:8},
{name:"cs",age:16}
]
var newValue = []
for(var i=0;i<arr.length;i++){
if(arr[i].age>16){
newValue.push(arr[i])
}
}
console.log(newValue);
判断数组中对象的age属性,如果大于16就将其添加到newValue数组中
var arr = [1,2,3,4,5,6,7,8,9]
var test = []
for(var i=0;i<arr.length;i += 3){
test.push(arr.slice(i,i+3))
}
console.log(test);
将其三个数一组,截取成三行三列的二维数组
查询数组中的最小值
1、
var arr = [4,6,18,1]
var min = arr[0]
console.log(arr.length);
for(var i=1;i<arr.length;i++){
if(min>arr[i]){
min = arr[i]
}
}
console.log(min);
2、
var arr = [4,6,18,1]
var min = arr[0]
for(var i=1;i<arr.length;i++){
min = Math.min(arr[0],arr[i])
}
3、
var arr = [4,6,18,1]
var min
for(var i=0;i<arr.length-1;i++){
for(var j=i+1;j<arr.length;j++){
if(arr[i]<arr[j]){
min = arr[i]
}else{
min = arr[j]
}
min = Math.min(arr[i],arr[j])
}
}
var arr = [
{name:'lisi',age:'12px'},
{name:'wangwu',age:'13px'},
{name:'zhangsan',age:'14px'}
]
for(var i=0;i<arr.length;i++){
var age = parseInt(arr[i].age)*2
arr[i].age = age
arr[i].like = true
if(arr[i].age<26){
arr[i].like = false
}
}
console.log(arr);
将数组arr中的对象属性age的值转换为数字
并在age大于24的对象中添加like=true属性
var searchs = [
{
spell:'A',
city:[
{name:'阿里',id:1001},
{name:'阿拉',id:1001},
{name:'阿克',id:1001}
]
},{
spell:'T',
city:[
{name:'天下',id:2001},
{name:'天天',id:2002},
{name:'天里',id:2003}
]
}
]
var input = document.getElementById('input')
var arr = []
var kw;
input.onkeydown = function(event){
if(event.keyCode === 13){
if(this.value != ""){
kw = this.value
addCity(kw)
console.log(arr);
this.value = ""
kw = this.value
arr = []
}
}
}
function addCity(keyword){
for(var i=0;i<searchs.length;i++){
var citys = searchs[i].city
for(var j=0;j<citys.length;j++){
console.log(citys[j]);
if(citys[j].name.includes(keyword)){
arr.push(citys[j])
}
}
}
}
在输入框中输入,获取到输入框中的value值,并通过数组的include方法判断这个值是否在数组的对象的那么属性的属性值中,
在的话,将这个对象保存到另一个数组中,输出