Javascript返回与原始数组不同的结构
我有一个对象数组,看起来像Javascript返回与原始数组不同的结构,javascript,node.js,algorithm,data-structures,Javascript,Node.js,Algorithm,Data Structures,我有一个对象数组,看起来像 const data: [ { id: 21786162, label: "cBTC 2021-06-25 Put $50,000.00", active": true, type: "put", strike_price: 5000000, date_live: "2019-11-19 05:00:00+0000", date_expires: "2021-06-25 20:
const data: [
{
id: 21786162,
label: "cBTC 2021-06-25 Put $50,000.00",
active": true,
type: "put",
strike_price: 5000000,
date_live: "2019-11-19 05:00:00+0000",
date_expires: "2021-06-25 20:00:00+0000",
date_exercise: "2021-06-25 21:00:00+0000",
},
{
id: 21786161,
label: "cBTC 2021-06-25 Call $50,000.00",
active": true,
type: "call",
strike_price: 5000000,
date_live: "2019-11-19 05:00:00+0000",
date_expires: "2021-06-25 20:00:00+0000",
date_exercise: "2021-06-25 21:00:00+0000",
},
{
id: 21786160,
label: "cBTC 2021-06-25 Put $25,000.00",
active": true,
type: "put",
strike_price: 2500000,
date_live: "2019-11-19 05:00:00+0000",
date_expires: "2021-06-25 20:00:00+0000",
date_exercise: "2021-06-25 21:00:00+0000",
},
{
id: 21786159,
label: "cBTC 2021-06-25 Call $25,000.00",
active": true,
type: "call",
strike_price: 2500000,
date_live: "2019-11-19 05:00:00+0000",
date_expires: "2021-06-25 20:00:00+0000",
date_exercise: "2021-06-25 21:00:00+0000",
}....//MORE ITEMS]
这些项目必须按到期日期和履约价格分组。然后有一种方法来区分该项是put类型还是call类型
我想要的是一个如下所示的数据结构:
const data = [
{
date_expires: "2021-06-25 20:00:00+0000",
strike_price: 5000000,
put: {
id: 21786162,
label: "cBTC 2021-06-25 Put $50,000.00",
active: true,
type: "put",
strike_price: 5000000,
date_live: "2019-11-19 05:00:00+0000",
date_expires: "2021-06-25 20:00:00+0000",
date_exercise: "2021-06-25 21:00:00+0000",
},
call: {
id: 21786161,
label: "cBTC 2021-06-25 Call $50,000.00",
active: true,
type: "call",
strike_price: 5000000,
date_live: "2019-11-19 05:00:00+0000",
date_expires: "2021-06-25 20:00:00+0000",
date_exercise: "2021-06-25 21:00:00+0000",
},
},
{
date_expires: "2020-09-25 20:00:00+0000",
strike_price: 1500000,
put: {
` id: 22199400,
label: "cBTC 2020-09-25 Put $15,000.00",
active: true,
type: "put",
strike_price: 1500000,
date_live: "2020-03-27 04:00:00+0000",
date_expires: "2020-09-25 20:00:00+0000",
date_exercise: "2020-09-25 21:00:00+0000",
},`
call: {
id: 22199399,
label: "cBTC 2020-09-25 Call $15,000.00",
active: true,
type: "call",
strike_price: 1500000,
date_live: "2020-03-27 04:00:00+0000",
date_expires: "2020-09-25 20:00:00+0000",
date_exercise: "2020-09-25 21:00:00+0000",
},
}.....
]
我设法找到了一个可行的解决方案,但代码似乎有点草率,我想知道是否可以改进它(我愿意使用Map或其他数据结构)
我的解决方案:
function groupBy(array, f) {
var groups = {};
array.forEach(function (o) {
var group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function (group) {
return groups[group];
});
}
const grouped = groupBy(contracts, function (item) {
return [item.date_expires, item.strike_price];
});
const final = grouped.map((el) => {
const result = {
date_expires: el[0].date_expires,
strike_price: el[0].strike_price,
};
if (el[0].type === 'put') {
result.put = el[0];
result.call = el[1];
} else if (el[0].type === 'call') {
result.put = el[1];
result.call = el[0];
}
return result;
});
我认为您的代码可以通过对可读性进行一些小的改进来得到一些改进,特别是使用
对象.values
,for..of
,您的groupBy还可以直接支持合并:
const toArray = (prev = [], el) => prev.concat(el);
function groupBy(array, getKey, merge = toArray) {
const groups = {};
for(const el of array) {
const key = JSON.stringify(getKey(el));
groups[key] = merge(groups[key], el);
}
return Object.values(groups);
}
这样,您的代码将变成:
const result = groupBy(
data,
it => [it.date_expires, it.strike_price],
(group = {}, { date_price, strike_price, ...el}) => ({ ...group, date_price, strike_price, [el.type]: el })
);
这真的更好吗?这主要取决于你的观点