1

I need an array of values to be created from a JSON. I am trying to achieve it using angular.forEach, sample plunk http://next.plnkr.co/edit/eXQ1gqO2DXzQKxFY which seems to not work! Please let me know whats incorrect or if there is any better method to achieve this. For example:

// Sample JSON
$scope.Data = {"Gate": [{"Entry": "One"},{"Entry": "Two"}]};
$scope.results = [];
$scope.results = ["One","Two"];

I need the results array.

Thank You!

1 Answer 1

1

Use Array.map

let Data = {"Gate": [{"Entry": "One"},{"Entry": "Two"}]};
let results = Data.Gate.map(v => v.Entry);
console.log(results);

Sign up to request clarification or add additional context in comments.

3 Comments

I made it work in the long way like this next.plnkr.co/edit/eXQ1gqO2DXzQKxFY Thanks Nikhil, the map function seems awesome. I am getting an error that tells 'map is not a function'. Are there any pre-requisites to use this? My app runs on Angular JS 1.3 Thanks!
@SruthiEmmidi - It works fine for me. See the updated plunker - next.plnkr.co/edit/u1lD3XpQbRKkKwli
Array.prototype.map was added in ECMAScript 5.1, if the browser you are using doesn't currently support it, then you would get the "map is not a function" error. If you look at the link included on the answer, there is a "browser compatibility" section near the bottom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.