最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】ES6中字典用map生成数组list

JS crifan 4278浏览 0评论

之前已有数组中是字典类型的变量:

  1. (3) [{…}, {…}, {…}]
    1. 0:{text: “Calf”, number: 34, color: “rgb(174,219,255)”}
    2. 1:{text: “Young cow”, number: 6, color: “rgb(63,120,165)”}
    3. 2:{text: “Heifer”, number: 0, color: “rgb(97,185,255)”}
    4. length:3
    5. __proto__:Array(0)

需要生成对应的list数组

不想用for in(或foreach)之类的

想要用map

所以去:

es6 map to array

Array.from() – JavaScript | MDN

Array.prototype.map() – JavaScript | MDN

javascript – Convert ES6 Iterable to Array – Stack Overflow

javascript – How to convert Map keys to array? – Stack Overflow

【总结】

最后用:

let legendTitleList = data.map(({text, number, color}) => text);

或:

  let legendTitleList = data.map((eachDict) => {
    return eachDict.text;
  });

即可输出对应的我要的字符串数组了:

  1. (3) [“Calf”, “Young cow”, “Heifer”]
    1. 0:”Calf”
    2. 1:”Young cow”
    3. 2:”Heifer”
    4. length:3
    5. __proto__:Array(0)

转载请注明:在路上 » 【已解决】ES6中字典用map生成数组list

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.165 seconds, using 22.06MB memory