gpt4 book ai didi

javascript - 使用 JavaScript 将对象转换为数组

转载 作者:行者123 更新时间:2023-11-28 18:39:28 25 4
gpt4 key购买 nike

在这些中,我需要添加养老金名称相同的数组值。如果pensionName 值相同,则添加所有金额,并将数组重新更改为myoutput 数组。

我的主数组:

Object, Object, Object, Object, Object, Object, Object]
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_1"
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_2"
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_3"
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_4"
Object
amount: 2000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_5"
Object
amount: 2000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_6"
Object
amount: 2000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_7"

我需要输出为:

[Object, Object]
Object
amount: 8000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_1"

Object
amount: 6000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_2"

所以我做了:

Enter code here
_.forEach(pension,function(item) {
key = item['pensionName'];
if (key != "0" && key !=""){
if (typeof(groups[key]) == "undefined") {
count++;
str = "row_"+count;
groups[key] = {'member' : item['member'],'name' : item['name'],'pensionName' : item['pensionName'],'amount' : item['amount'],'rowId' : str};
}
else {
groups[key]['amount'] = parseFloat(groups[key]['amount']) + parseFloat(item['amount']);
}
}
});

结果是:

1A:Object, 2A: Object]
1A: Object
amount :200
member :1
name : "hello"
name2 : 1A
id : "row_1"
2A: Object
amount : 200
member : 2
name : "hello"
name2 : "2A"
id : "row_2"

但我无法将其插入数组:

var pensionBalance = getPension();
pensionBalance.map(function(v){
arr.push(v);
});

那么如何将其转换为数组并推送或任何其他方法来添加数组值?

最佳答案

也许这对您有用,但我不确定如何处理 rowId 属性。采取全部还是只采取第一个?

Basically this solution works with the thisArg, here this from Array#forEach. this is used as a temporary object with references to array items of unique persons in grouped.

In the forEach loop, first it checks if the person is already in the grouped array and therfore in this. If not, a new object is generated and to a new property with the actual pensionName to this assigned and pushed to grouped.

Now we can access this object with the key of pensionName and add the amount and push rowId to the array for it.

temporary this object:

{
"1A": { // generated in first loop
"amount": 8000,
"member": "1",
"name": "Peter Andrews",
"pensionName": "1A",
"rowId": [
"row_1", // added in first
"row_2", // ... second
"row_3", // third
"row_4" // forth
]
},
"2A": { // generated in fifth loop
"amount": 6000,
"member": "2",
"name": "Peter Andrews",
"pensionName": "2A",
"rowId": [
"row_5", // added in fifth
"row_6", // ... sixth
"row_7" // seventh
]
}
}

var object = [{ amount: 2000, member: "1", name: "Peter Andrews", pensionName: "1A", rowId: "row_1" }, { amount: 2000, member: "1", name: "Peter Andrews", pensionName: "1A", rowId: "row_2" }, { amount: 2000, member: "3", name: "Peter Andrews", pensionName: "1A", rowId: "row_3" }, { amount: 2000, member: "1", name: "Peter Andrews", pensionName: "1A", rowId: "row_4" }, { amount: 2000, member: "2", name: "Peter Andrews", pensionName: "2A", rowId: "row_5" }, { amount: 2000, member: "2", name: "Peter Andrews", pensionName: "2A", rowId: "row_6" }, { amount: 2000, member: "2", name: "Peter Andrews", pensionName: "2A", rowId: "row_7" }],
grouped = [];

object.forEach(function (a) {
if (!this[a.pensionName]) {
this[a.pensionName] = { amount: 0, member: a.member, name: a.name, pensionName: a.pensionName, rowId: [] };
grouped.push(this[a.pensionName]);
}
this[a.pensionName].amount += a.amount;
this[a.pensionName].rowId.push(a.rowId);
}, {});

document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

关于javascript - 使用 JavaScript 将对象转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36363964/

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