简体   繁体   English

从数组中获取特定值

[英]Get specific values out of an array

I'm trying to write a little script, which gets me the name of a tag and it's ID out of Asana. 我正在尝试编写一个小脚本,该脚本让我获得标签的名称以及Asana的ID。 I've an original array structured like this: 我有一个像这样的原始数组:

"data":[{"id":"27724930904,"gid":"27724930904","name":"Tag1"},
{"id":"26724930954,"gid":"26724930954","name":"Tag2"},
{"id":"26109930621,"gid":"26109930621","name":"Tag3"}]}

The goal is, to get two arrays like this: 目的是要获得两个这样的数组:

var tagName = ["Tag1", "Tag2", "Tag3"];
var tagID = ["27724930904", "26724930954", "26109930621"];

The "gid"-number is not needed. 不需要“ gid”号。 It would be great, if the script would get the original array directly from it's URL ( https://app.asana.com/api/1.0/tags ). 如果脚本可以直接从其URL( https://app.asana.com/api/1.0/tags )中获取原始数组,那就太好了。

I'm quite new in javascript and even struggling to get the original array from the URL. 我在javascript方面还很陌生,甚至还在努力从URL获取原始数组。

Any hints how this could work? 有任何提示如何运作吗? Thanks for your help guys. 感谢您的帮助。

I think you want this: 我想你想要这个:

var asana = { "data": 
  [
    { "id": "27724930904", "gid": "27724930904", "name": "Tag1" }, 
    { "id": "26724930954", "gid": "26724930954", "name": "Tag2" }, 
    { "id": "26109930621", "gid": "26109930621", "name": "Tag3" }
  ]
};

var tagName = asana.data.map(item => item.name);
var tagID = asana.data.map(item => item.id);

JSFiddle: https://jsfiddle.net/charlesartbr/xehcrps0/6/ JSFiddle: https ://jsfiddle.net/charlesartbr/xehcrps0/6/

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

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