在Javascript生成器函数中设置键 [英] Set key in a Javascript generator function

查看:75
本文介绍了在Javascript生成器函数中设置键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在符号迭代器中设置密钥,这是我到目前为止的代码:

Hi I'm trying to figure out how to set up the key in a symbol iterator, this is the code I have so far:

let james = {
    name: 'James',
    height: `5'10"`,
    weight: 185
};

james[Symbol.iterator] = function* () {
    for (let key in this) {
        yield this[key];
    }
}

let iterator = james[Symbol.iterator]();
console.log(iterator.next().value); // 'James'
console.log(iterator.next().value); // `5'10`
console.log(iterator.next().value); // 185

例如,我遇到的问题是: iterator.next()的调用应打印

the issue I'm having is for example: the call of iterator.next() should print

{"value": "James", "key": "name", "done": false}

但是我得到

{"value": "James", "done": false}

我想以某种方式设置键",就像设置值"一样.

I'd like to set somehow the "key" as the same way I'm setting up the "value".

我查看了文档,但是我还没有看到与此相关的任何文档.

I checked to the documentation, but I haven't seen any docs related to this.

有什么想法吗?

编辑

该问题的用例基本上是将 James对象转换为可迭代对象,如何"无关紧要,所以我的第一个尝试是使用生成器,然后我意识到我需要以这种格式打印对象:

the use case for the question was basically turning the James object into an iterable object, it doesn't matter "how", so my first try was to use a generator, then I realized I needed to print the object in this format:

{ value: 'James', key: 'name', done: false }
{ value: '5\'10"', key: 'height', done: false }
{ value: 185, key: 'weight', done: true }

这不是标准的方法,所以我不得不创建一个自定义方法来实现"行为:

which is not a standard way to do it, so I had to create a custom method to "implement" the behavior:

由于@loganfsmyth为我指出了正确的方向,所以我提出了一个简单的解决方案:

thanks to @loganfsmyth for pointing me in the right direction, I came up with this simple solution:

let james = {
    name: 'James',
    height: `5'10"`,
    weight: 185
};

james[Symbol.iterator] = function (){
   const keys = [];
   for (let key in this) {
      keys.push({'key':key, 'value':this[key]});
    }
  return {
    next(){
      let {key,value} = keys.shift();
      return {value,key,done:keys.length===0};
    }
  }
}

let iterator = james[Symbol.iterator]();
console.log(iterator.next().value); // 'James'
console.log(iterator.next().value); // `5'10`
console.log(iterator.next().value); // 185

推荐答案

如果要同时生成键和值,则需要生成包含两个项目的数组,例如

If you want to yield both the key and the value, you'd need to yield an array with two items, e.g.

yield [key, this[key]];

然后该对象上的.value将是一个键/值对.

then the .value on the object would be a key/value pair.

还请注意,此行为与从Object.entries(james)获得的行为类似,该行为是ES2017中添加的新功能,只是它返回Array.

Also note that this behavior is similar to what you get from Object.entries(james) which is a new function added in ES2017, except that it returns an Array.

如果明确想要

.next()

要返回一个同时包含keyvaluedone的对象,那么答案是您不能使用生成器来做到这一点.那不是生成器提供的API,也不是ES6中定义的迭代器协议,因此您不应该使用迭代器来尝试执行此操作.

to return an object with both key, value and done, then the answer is you can't do that with a generator. That's not the API that generators provide, and it's not the iterator protocol defined in ES6, so you shouldn't be using an iterator to try to do this.

您当然可以

james.makeCustomThing = function(){ 
  const pairs = [];
  for (const key in this) pairs.push([key, this[key]]);
  return {
    next(){
      if (pairs.length === 0) return {done: true};

      let [key, value} = pairs.shift();
      return {key, value, done: false};
    },
  };
};

但是它不再是迭代器,而是您自己定义的自定义内容.

but then it's not an iterator anymore, it's just a custom thing you've defined yourself.

这篇关于在Javascript生成器函数中设置键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆