关于数组的一些
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var users = [
{name: "张含韵", age: "12"},
{name: "江一燕", age: "21"},
{name: "李小璐", age: "32"}
];
var aall = [
{name: "11张含韵", age: "1112"},
];
// var newage=[];//遍历相加
// users.map(function(val,index){
// newage.push(parseInt(val.age) + 10);//遍历加10
// return newage
// });
// console.log(newage)//[22, 31, 42]
// var cont= users.concat(aall);//合并两个数组
// console.log(cont)
// var twoobj={name: "1111"};
// var threeobj={age: "116"};
// var oneobj=Object.assign(twoobj,threeobj);//{name: "1111", age: "116"}
// console.log(oneobj)
var a=["12","122","132","162"]
a.splice(2,0,"Lemon","wedd");//冲第二个位置开始添加,不删除,只添加因为第二个参数为0["12","122","Lemon","wedd","132","162"]
// a.splice(1,2,"Lemon","wedd");//冲第1个位置开始添加,并删除紧接后两个,["12", "Lemon", "wedd", "162"]
console.log(a);
// 两个对象合并并转为数组
var jsonArr = [
{
"ID": "",
"NO": "001",
"NAME": "测试对象1"
},
{
"ID": "",
"NO": "002",
"NAME": "测试对象2"
},
{
"ID": "",
"NO": "003",
"NAME": "测试对象3"
},
{
"ID": "",
"NO": "004",
"NAME": "测试对象4"
},
{
"ID": "",
"NO": "005",
"NAME": "测试对象5"
}
];
var jsonArr2 = [
{
"ID": "",
"NO": "003",
"NAME": "测试对象3"
},
{
"ID": "",
"NO": "004",
"NAME": "测试对象4"
},
{
"ID": "",
"NO": "006",
"NAME": "测试对象6"
},
{
"ID": "",
"NO": "007",
"NAME": "测试对象7"
}
];
var length1 = jsonArr.length;
var length2 = jsonArr2.length;
for (var i = 0; i < length1; i++) {
for (var j = 0; j < length2; j++)
{
//判断添加的数组是否为空了
if (jsonArr.length > 0) {
if (jsonArr[i]["NAME"] === jsonArr2[j]["NAME"]) {
jsonArr.splice(i, 1); //利用splice函数删除元素,从第i个位置,截取长度为1的元素
length1--;
}
}
}
}
for (var n = 0; n < jsonArr2.length; n++) {
jsonArr.push(jsonArr2[n]);
}
// console.log(JSON.stringify(jsonArr));
newArr = users.filter((item, index, arr) => item.name == "李小璐")
console.log(newArr)
// 根据单个名字筛选
function filterByName(aim, name) {
return aim.filter(item => item.name == name)
}
// 输入 aim 'Leila' 期望输出为 {name:'李小璐', age: "32"}
console.log(filterByName(users,'李小璐'))
</script>
</body>
</html>