最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I change the value of an item in a Set? - Stack Overflow

programmeradmin2浏览0评论

I have a Set that I want to update in JavaScript, but I can't figure out anything that lets me update values within it.

const mySet = new Set([]);
mySet.add('hello');
mySet.add('hewwo');
mySet.add('henlo');

I've tried syntax like

mySet['hello'] = 'hello world',

mySet.hello = 'hello world', and

mySet[0] = 'hello world'.

I've also tried modifying the value like this:

mySet.forEach(item => {
  if (item === 'hello') {
    item = 'hello world';
  }
})

Nothing has worked for me so far though. I wanted to use a set over a normal array because it prevents duplicate items automatically, but should I just add the logic myself? Is there a good use case for sets in JavaScript if I can't modify the values in them?

I have a Set that I want to update in JavaScript, but I can't figure out anything that lets me update values within it.

const mySet = new Set([]);
mySet.add('hello');
mySet.add('hewwo');
mySet.add('henlo');

I've tried syntax like

mySet['hello'] = 'hello world',

mySet.hello = 'hello world', and

mySet[0] = 'hello world'.

I've also tried modifying the value like this:

mySet.forEach(item => {
  if (item === 'hello') {
    item = 'hello world';
  }
})

Nothing has worked for me so far though. I wanted to use a set over a normal array because it prevents duplicate items automatically, but should I just add the logic myself? Is there a good use case for sets in JavaScript if I can't modify the values in them?

Share Improve this question edited Sep 18, 2019 at 6:13 Gayan S. Muthukumarana 9041 gold badge11 silver badges29 bronze badges asked Sep 18, 2019 at 6:08 Christopher BradshawChristopher Bradshaw 2,7735 gold badges27 silver badges41 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

The only way to "modify" a (primitive) item would be to remove it from the Set and then add the altered item.

const mySet = new Set([]);
mySet.add('hello');
mySet.add('hewwo');
mySet.add('henlo');

if (mySet.has('hello')) {
  mySet.delete('hello');
  mySet.add('hello world');
}

console.log(...mySet);

If you want to change each item, either iterate over each item, deleting and adding again, or (as would probably be easier) transform it into an array and .map into a new Set.

While you can do this, it's still a pretty weird thing to want to do. It's useful to think of Sets as unordered collections, which can have values individually added or removed.

One big reason sets are useful is that looking up values in them is an O(1) operation, unlike with arrays (where lookup requires O(n) time).

In Set you need to delete the previously available value and than add new one if you want to replace already existing value,

One alternative when you want to replace key's value regularly you can use Map

const myMap = new Map([]);
myMap.set('hello', 'hello');
myMap.set('hewwo', 'hewwo');
myMap.set('hello', 'Hello world')

console.log([...myMap.values()].join('\n')); 

// join('\n') is just for formatting output

The Set lets you store unique values

For specified key and a value pairs use Map

const myMap = new Map();
myMap.set('hello', 'hello');
myMap.set('hewwo', 'hewwo');
myMap.set('henlo', 'henlo');

myMap.set('hello', 'hello world');

console.log(myMap.get('hello'))

console.log(...myMap.entries())

I had a set and I wanted to update values that may contains duplicate during update. This is what I did

let test = new Set([1,2,3]);
let tmpSet = new Set();
for (let num of test) {
    tmpSet.add(num+1);
    
}
test = tmpSet;
console.log(tmpSet); // [2,3,4]
发布评论

评论列表(0)

  1. 暂无评论