有一个对象数组如下。需要仅使用VatCode propery从跟随的对象数组创建一个数组而不通过'for循环'。
这里的结果应该是
resultArray = [1,15,25]
"Data": [
{
"Id": 1,
"VatCode": 1,
"Name": "Ingen mva.",
"VatAccount": {
"ID": 3,
"AccountNo": "3190",
"Name": "Misc Items Income"
}
},
{
"Id": 2,
"VatCode": 15,
"Name": "Lav mva.",
"VatAccount": {
"ID": 3,
"AccountNo": "3190",
"Name": "Misc Items Income"
}
},
{
"Id": 3,
"VatCode": 25,
"Name": "Høy mva.",
"VatAccount": {
"ID": 3,
"AccountNo": "3190",
"Name": "Misc Items Income"
}
},
]
答案 0 :(得分:1)
Data = //your json data
this.Data.map(data => data.VatCode);
map()方法创建一个新数组,其中包含调用a的结果 每个数组元素的函数。
var data = [
{
"Id": 1,
"VatCode": 1,
"Name": "Ingen mva.",
"VatAccount": {
"ID": 3,
"AccountNo": "3190",
"Name": "Misc Items Income"
}
},
{
"Id": 2,
"VatCode": 15,
"Name": "Lav mva.",
"VatAccount": {
"ID": 3,
"AccountNo": "3190",
"Name": "Misc Items Income"
}
},
{
"Id": 3,
"VatCode": 25,
"Name": "Høy mva.",
"VatAccount": {
"ID": 3,
"AccountNo": "3190",
"Name": "Misc Items Income"
}
},
];
var s = data.map(data => data.VatCode);
console.log(s);