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

ecmascript 6 - How to get individual key value from “Set” Object in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I have Set() object with three key value

var myset =new Set();
myset.add('first','This is first value');
myset.add('second','This is second value');
myset.add('third','This is third value');

Using loop I can got value of these three key

for( var value of myset){
  console.log(value);
}

How can get individual value? I want to get second key of value?

Is there any option?

I have tried these but not working

myset.get('second');
myset(1);

var myset = new Set();
myset.add('first', 'This is first value');
myset.add('second', 'This is second value');
myset.add('third', 'This is third value');

//   Using loop I can got value of these three key

for (var value of myset) {
  console.log(value);
}

// How can get individual value? I want to get second key of value?

// I have tried these but not working

myset.get('second');
myset(1);

I have Set() object with three key value

var myset =new Set();
myset.add('first','This is first value');
myset.add('second','This is second value');
myset.add('third','This is third value');

Using loop I can got value of these three key

for( var value of myset){
  console.log(value);
}

How can get individual value? I want to get second key of value?

Is there any option?

I have tried these but not working

myset.get('second');
myset(1);

var myset = new Set();
myset.add('first', 'This is first value');
myset.add('second', 'This is second value');
myset.add('third', 'This is third value');

//   Using loop I can got value of these three key

for (var value of myset) {
  console.log(value);
}

// How can get individual value? I want to get second key of value?

// I have tried these but not working

myset.get('second');
myset(1);

Share Improve this question edited Jul 12, 2018 at 11:46 mplungjan 178k28 gold badges180 silver badges240 bronze badges asked Jul 12, 2018 at 11:44 Jitendra KumarJitendra Kumar 2,2212 gold badges19 silver badges23 bronze badges 3
  • Nothing here? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – mplungjan Commented Jul 12, 2018 at 11:47
  • 3 Set.add takes only one parameter – Hikmat G. Commented Jul 12, 2018 at 11:48
  • 2 What you need is developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – Hikmat G. Commented Jul 12, 2018 at 11:49
Add a comment  | 

5 Answers 5

Reset to default 7

Set is a set of values, not a map of them, so add accepts 1 argument, which is a value. The thing you're looking for is Map.

Getting a value by its index is explained in this answer and it's same for both Set and Map. A collection should be iterated until the index is reached. This can be done in less efficient manner by converting it to an array first:

const map = new Map();
map.set('first','This is first value');
map.set('second','This is second value');
map.set('third','This is third value');

console.log(map.get('second') === 'This is second value'); // true
console.log([...map.values()][1] === 'This is second value'); // true

Javascript Set.add() only takes one parameter. So your second param is always ignored, and as its JS this doesnt throw any error. You can achieve what you are trying using simple JS object.

var x = {}
x[key1] = value1
x[key2] = value2

And then you can get the values like:

x[key1] # will output value1

You're using a Set wrong. It's not a key/value store, but a list of unique values.

Source

Your code...

var myset =new Set();
myset.add('first','This is first value');
myset.add('second','This is second value');
myset.add('third','This is third value');

Is really creating a set with values "first", "second", "third" (the second argument is ignored, since Set.prototype.add() only takes one argument).

For a generic key/value store, you should use either a Map or a regular JavaScript object (depending on your supported environment).

Map

A map lets you store key/value pairs where the key can be any type. This is different to a regular JS object where the key must be of string type.

const myMap = new Map();
myMap.set('key', 'value');

Regular JS Object

You can use a regular JS object, since your keys are strings, preferably with a null prototype available via Object.create(null) (the null prototype ensures you won't read any other values back on the prototype chain without having to do a Object.prototype.hasOwnProperty() check).

Set.add only take one parameter. What you can do is, Insert JSON object instead shown as below:

var myset = new Set();
myset.add({'first' : 'This is first value'});
myset.add({'second': 'This is second value'});
myset.add({'third':'This is third value'});

var val = [...myset].filter(x => x.hasOwnProperty('first'))[0]['first'];
console.log(val)

Because the Set object is implemented as a generator is not possible to get its values by index straight away, although there are two options to achieve this.

1.- Unpack the Set generator and retrieve the value by index.

function list(setobj) {
    var result = []
    for (var value of setobj){
       result.push(value)
    }
    return result
}
asd = new Set([1,2,3,3,4,4])
list(asd.values())[1] 
// 2

2.-Implement your own Set subclassing Array

function set(){
    var args = arguments
    if (arguments[0] instanceof Array) {
        args = arguments[0]
    }
    for (var index in args){
        self.add(args[index])
    }
}
set.prototype = new Array
set.prototype.add = function (el) {
    if (this.indexOf(el) == -1) this.push(el)
}
asd = new set(1,1,1,2,2,3)
console.log(asd[1])
// 2
asd = new set([1,1,1,2,2,3])
console.log(asd[1])
// 2
发布评论

评论列表(0)

  1. 暂无评论