在一个名为datas的json数组对象中有id,name,age,怎么取出其中的id和name拼接成一个新的数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
</style>
</head>
<body>
<button onclick="addArr()">生成新的数据</button>
</body>
<script type="text/javascript">
var datas = [
{ "id":1 , "Name":"Gates","age":34 },
{ "id":2 , "Name":"Bush","age":22 },
{ "id":3 , "Name": "Carter","age":44 }
];
var newArr = [];
function addArr() {
for (var i = 0; i <datas.length; i++) {
var vote = {};
vote.id = datas[i].id;
vote.Name = datas[i].Name;
newArr.push(vote);
}
console.log(datas);
console.log(newArr);
}
</script>
</html>
在微信小程序中
var depart_list = [{
"id": 1,
"Name": "Gates",
"age": 34
},
{
"id": 2,
"Name": "Bush",
"age": 22
},
{
"id": 3,
"Name": "Carter",
"age": 44
}
];
var depart_lists = [];
for (var i = 0; i < depart_list.length; i++) {
var vote = {};
vote.id = depart_list[i].id;
vote.Name = depart_list[i].Name;
depart_lists.push(vote);
}
console.log(depart_list);
console.log(depart_lists);
(3) [{…}, {…}, {…}]
0: {id: 1, Name: "Gates", age: 34}
1: {id: 2, Name: "Bush", age: 22}
2: {id: 3, Name: "Carter", age: 44}
length: 3
nv_length: (...)
__proto__: Array(0)
register_details.js? [sm]:158
(3) [{…}, {…}, {…}]
0: {id: 1, Name: "Gates"}
1: {id: 2, Name: "Bush"}
2: {id: 3, Name: "Carter"}
length: 3
nv_length: (...)
__proto__: Array(0)