简体   繁体   English

如何在 Javascript 集合 object 中的特定索引处添加元素?

[英]How do I add an element at a specific index in Javascript set object?

I tried using options.splice(index, 0, value);我尝试使用options.splice(index, 0, value); but it only works for arrays not for set objects.但它仅适用于 arrays 不适用于设置对象。 Don't suggest ES6 ways please.请不要建议 ES6 方式。

You can convert the set to an array, do what you want and convert it back to a set.您可以将集合转换为数组,做您想做的事情并将其转换回集合。

// Assuming options is a Set
var optionsArray = Array.from(options);
optionsArray.splice(index, 0, value);
options = new Set(optionsArray);

ES6 even though you don't want it:) ES6,即使你不想要它:)

// Assuming options is a Set
const optionsArray = [...options];
optionsArray.splice(index, 0, value);
options = new Set(optionsArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM