gpt4 book ai didi

javascript - 从具有相同键的对象创建一个新数组

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

我有一个对象数组。我想将对象合并到一个数组中,并使用其中一个值作为键。在下面的示例中,我有一个数据数组,我从服务器获取该数据数组作为对 API 的响应,并且我想使用 call_id 作为将响应索引到新数组的键。

我尝试过:
data.map(function(index, elem) {responses[index.call_id] = index;})
但这显然只能获取最后一个数组,并且添加 [] 会给我一个错误

当前数组:

[
{
"id": 2,
"survey_id": 1,
"question_id": 2,
"response": 1,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:47",
"updated_at": "2020-02-20 18:18:47",
"question": "Do you want it gift wrapped?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]

预期结果

[{
'108': [
{
"id": 2,
"survey_id": 1,
"question_id": 2,
"response": 1,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:47",
"updated_at": "2020-02-20 18:18:47",
"question": "Do you want it gift wrapped?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 108,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
],
'109' : [
{
"id": 1,
"survey_id": 1,
"question_id": 1,
"response": 2,
"order_id": null,
"customer_id": 1,
"call_id": 109,
"created_at": "2020-02-20 18:18:32",
"updated_at": "2020-02-20 18:18:32",
"question": "Is your order confirmed?",
"first_name": "Zain",
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]
}]

最佳答案

我认为您想要如下所示的内容,对吗?

如果是的话,让我解释一下:
您将使用 .reduce(),它使用两个值作为参数,一个累加器(在本例中是一个对象)和正在迭代的当前值(在本例中是数组中的每个对象)

每次迭代都会检查累加器,看看当前迭代对象的call_id是否已经存在,如果存在,则将对象插入其中,如果不存在,则创建一个新对象以 call_id 作为键。

注意:我使用属性较少的数组只是为了更好地可视化代码

let arr = [{
"id": 2,
"call_id": 108,
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 1,
"call_id": 108,
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
},
{
"id": 3,
"call_id": 109,
"sid": "CA1564cda12b7e1364dc967538c7bdf617"
}
]


let result = arr.reduce(function(accObj, currentObj) {
accObj[currentObj.call_id] = accObj[currentObj.call_id] || [];
accObj[currentObj.call_id].push(currentObj);
return accObj;
}, {}); //{} is the accumulator object

console.log(result);

关于javascript - 从具有相同键的对象创建一个新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327038/

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