-1

I'm trying to write a little script, which gets me the name of a tag and it's ID out of Asana. 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. 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).

I'm quite new in javascript and even struggling to get the original array from the URL.

Any hints how this could work? Thanks for your help guys.

martin1009
  • 25
  • 5
  • 1
    Have you made any attempt at all so far to accomplish that yourself? Please post what you've tried. Also, your `tagName` and `tagID`s syntax is invalid. – CertainPerformance Sep 03 '18 at 21:43
  • Read up on how to use [`Array#map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) – charlietfl Sep 03 '18 at 22:32

1 Answers1

2

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/

Charles Cavalcante
  • 1,572
  • 2
  • 14
  • 27