0

There is an array of objects as follows. Need to create an array only using VatCode propery from following array of objects without going through a 'for loop'. Here result should be
resultArray = [1, 15, 25]

"Data": [
    {
        "Id": 1,
        "VatCode": 1,
        "Name": "Ingen mva.",            
        "VatAccount": {
            "ID": 3,
            "AccountNo": "3190",
            "Name": "Misc Items Income"
        }            
    },
    {
        "Id": 2,
        "VatCode": 15,
        "Name": "Lav mva.",           
        "VatAccount": {
            "ID": 3,
            "AccountNo": "3190",
            "Name": "Misc Items Income"
        }
    },
    {
        "Id": 3,
        "VatCode": 25,
        "Name": "Høy mva.",           
        "VatAccount": {
            "ID": 3,
            "AccountNo": "3190",
            "Name": "Misc Items Income"
        }
    },

]
Adikari Nadeesha
  • 322
  • 3
  • 12
  • Congratuations--you have won the special badge for most typos in one question title. By the way, this question has nothing to do with either Angular or JSON. –  Jul 26 '17 at 11:09

1 Answers1

1
Data = //your json data    

this.Data.map(data => data.VatCode);

The map() method creates a new array with the results of calling a function for every array element.

var data = [
    {
        "Id": 1,
        "VatCode": 1,
        "Name": "Ingen mva.",            
        "VatAccount": {
            "ID": 3,
            "AccountNo": "3190",
            "Name": "Misc Items Income"
        }            
    },
    {
        "Id": 2,
        "VatCode": 15,
        "Name": "Lav mva.",           
        "VatAccount": {
            "ID": 3,
            "AccountNo": "3190",
            "Name": "Misc Items Income"
        }
    },
    {
        "Id": 3,
        "VatCode": 25,
        "Name": "Høy mva.",           
        "VatAccount": {
            "ID": 3,
            "AccountNo": "3190",
            "Name": "Misc Items Income"
        }
    },

];

var s = data.map(data => data.VatCode);
console.log(s);
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86