-2

I have a JSON object in a JavaScript function:

[{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}]

That is saved to an Multi-dimensional Array

Is there a better way that I can make a make a new Array with only the PMID element without looping and building it. I know that this question is close to a duplicate but I'm not interested in a merge or a concat. Currently I'm doing

      var newArray = [];
      for (var i = 0; i < this.pmidList.length; i++) {
        newArray.push(this.pmidList[i]["PMID"]);
      }

This question maybe a duplicate but if the average person can't find it based upon the title then it should not be considered a duplicate. The solution is the same but the question titles are much different.

Bill
  • 1,423
  • 2
  • 27
  • 51

1 Answers1

5

You can use the Array.map function.

const input = [{"PMID":31206477,"MemberID":1287,"recID":6352},
  {"PMID":31202264,"MemberID":1245,"recID":5974},
  {"PMID":31201299,"MemberID":1184,"recID":3012},
  {"PMID":31196160,"MemberID":1241,"recID":3833}];

const output = input.map(item => item.PMID);

Documentation

ChrisG
  • 2,637
  • 18
  • 20
  • Thanks for the answer- I was sure this was a duplicate question but I could never find it. Hopefully my title will make it a bit easier to find. – Bill Oct 10 '19 at 00:56