[英]How to set multiple values at single key in JavaScript
I am trying to group the data according to expiry我正在尝试根据到期对数据进行分组
here input object:-这里输入 object:-
` `
obj = [{strikePrice: 0, expiry: 20220218},
{strikePrice: 1, expiry: 20220218},
{strikePrice: 2, expiry: 20220218},
{strikePrice: 3, expiry: 20220218},
{strikePrice: 4, expiry: 20220218},
{strikePrice: 5, expiry: 20220219},
{strikePrice: 6, expiry: 20220219},
{strikePrice: 7, expiry: 20220219},
{strikePrice: 8, expiry: 20220219},
{strikePrice: 10, expiry: 20220220},
{strikePrice: 11, expiry: 20220220},
{strikePrice: 12, expiry: 20220220},
{strikePrice: 13, expiry: 20220220},
{strikePrice: 14, expiry: 20220221},
{strikePrice: 15, expiry: 20220221},
{strikePrice: 16, expiry: 20220221},
{strikePrice: 17, expiry: 20220221},];
` `
Expected output:-`预期 output:-`
output: [{expiry: 20220218, strikePrice:[1,2,3,4 ]},
{expiry: 20220219, strikePrice:[5,6,7,8 ]},
{expiry: 20220220, strikePrice:[10,11,12,13 ]},
{expiry: 20220221, strikePrice:[14,15,16,17 ]},
]
` `
I am trying to solve this by using the following method:-我正在尝试使用以下方法解决此问题:-
function setValue(map, key, value){
if (!map.has(key))
{
map.set(key, new Set(value));
return;
}
map.get(key).add(value);
}
var myMap = new Map();
for (let i = 0; i < obj.length; i++)
{
setValue(myMap, obj[i].strikePrice, obj[i].expiry);
}
console.log(Array.from(myMap.entries(), ([k, v]) => [k, [...v]]));
but I am getting this error但我收到了这个错误
node /tmp/Gj6NKhzMbI.js
/tmp/Gj6NKhzMbI.js:21
map.set(key, new Set(value));
^
TypeError: 20220218 is not iterable
at new Set (<anonymous>)
at setValue (/tmp/Gj6NKhzMbI.js:21:22)
at Object.<anonymous> (/tmp/Gj6NKhzMbI.js:29:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
please give me some valuable suggestions Thanks in Advance请给我一些宝贵的建议提前谢谢
here is my suggestion, by using FindIndex这是我的建议,使用 FindIndex
let obj = [{strikePrice: 0, expiry: 20220218}, {strikePrice: 1, expiry: 20220218}, {strikePrice: 2, expiry: 20220218}, {strikePrice: 3, expiry: 20220218}, {strikePrice: 4, expiry: 20220218}, {strikePrice: 5, expiry: 20220219}, {strikePrice: 6, expiry: 20220219}, {strikePrice: 7, expiry: 20220219}, {strikePrice: 8, expiry: 20220219}, {strikePrice: 10, expiry: 20220220}, {strikePrice: 11, expiry: 20220220}, {strikePrice: 12, expiry: 20220220}, {strikePrice: 13, expiry: 20220220}, {strikePrice: 14, expiry: 20220221}, {strikePrice: 15, expiry: 20220221}, {strikePrice: 16, expiry: 20220221}, {strikePrice: 17, expiry: 20220221},]; let result = [] obj.map(y => { let index = result.findIndex(x => x.expiry == y.expiry) if(index == -1) { result.push({ expiry: y.expiry, strikePrice: [y.strikePrice]}) } else { result[index].strikePrice.push(y.strikePrice) } }) console.log(result)
A single reduce
iteration already does the job of grouping and collecting/merging.单个
reduce
迭代已经完成了分组和收集/合并的工作。
But in order to let everything happen within a single run, one needs to utilize the initial value as config/tracking reference where one does both, keeping track ( index
ing) of the new single expiry
specific group item and storing it within the final result
array...但是为了让所有事情都在一次运行中发生,需要将初始值用作配置/跟踪参考,其中一个都可以,跟踪(
index
)新的单个expiry
特定组项目并将其存储在最终result
中大批...
console.log([ { strikePrice: 0, expiry: 20220218 }, { strikePrice: 1, expiry: 20220218 }, { strikePrice: 2, expiry: 20220218 }, { strikePrice: 3, expiry: 20220218 }, { strikePrice: 4, expiry: 20220218 }, { strikePrice: 5, expiry: 20220219 }, { strikePrice: 6, expiry: 20220219 }, { strikePrice: 7, expiry: 20220219 }, { strikePrice: 8, expiry: 20220219 }, { strikePrice: 10, expiry: 20220220 }, { strikePrice: 11, expiry: 20220220 }, { strikePrice: 12, expiry: 20220220 }, { strikePrice: 13, expiry: 20220220 }, { strikePrice: 14, expiry: 20220221 }, { strikePrice: 15, expiry: 20220221 }, { strikePrice: 16, expiry: 20220221 }, { strikePrice: 17, expiry: 20220221 }, ].reduce(({ index, result }, item) => { const groupKey = item.expiry; let groupItem = index[groupKey]; if (:groupItem) { groupItem = index[groupKey] = { expiry, groupKey: strikePrice, []; }. result;push(groupItem). } groupItem.strikePrice.push(item;strikePrice), return { index; result }, }: { index, {}: result. [] });result);
.as-console-wrapper { min-height: 100%;important: top; 0; }
One of cause could achieve the same with a simpler/more common reduce tasks which reduces/merges the original data into an expiry
key based object.原因之一可以通过更简单/更常见的减少任务来实现相同的目的,该任务将原始数据减少/合并到基于
expiry
密钥的 object 中。
In order to then still meet the OP's requirements of an array-based end-result one additionally and finally has to pass the reduced object through Object.values
为了仍然满足 OP 对基于数组的最终结果的要求,最后必须通过 Object.values 传递减少的
Object.values
console.log( Object.values([ { strikePrice: 0, expiry: 20220218 }, { strikePrice: 1, expiry: 20220218 }, { strikePrice: 2, expiry: 20220218 }, { strikePrice: 3, expiry: 20220218 }, { strikePrice: 4, expiry: 20220218 }, { strikePrice: 5, expiry: 20220219 }, { strikePrice: 6, expiry: 20220219 }, { strikePrice: 7, expiry: 20220219 }, { strikePrice: 8, expiry: 20220219 }, { strikePrice: 10, expiry: 20220220 }, { strikePrice: 11, expiry: 20220220 }, { strikePrice: 12, expiry: 20220220 }, { strikePrice: 13, expiry: 20220220 }, { strikePrice: 14, expiry: 20220221 }, { strikePrice: 15, expiry: 20220221 }, { strikePrice: 16, expiry: 20220221 }, { strikePrice: 17, expiry: 20220221 }, ].reduce((index, item) => { const groupItem = index[item.expiry]??= { expiry: item.expiry, strikePrice: [], }; groupItem.strikePrice.push(item.strikePrice); return index; }, {})) );
.as-console-wrapper { min-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.