gpt4 book ai didi

javascript - 使用对象数组的特定属性创建数组

转载 作者:行者123 更新时间:2023-12-03 03:47:24 24 4
gpt4 key购买 nike

有一个对象数组,如下所示。需要仅使用以下对象数组中的 VatCode 属性创建一个数组,而无需通过“for 循环”。这里的结果应该是
结果数组 = [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"
}
},

]

最佳答案

Data = //your json data    

this.Data.map(data => data.VatCode);

The map() method creates a new array with the results of calling a function for every array element.

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);

关于javascript - 使用对象数组的特定属性创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com