0

I am trying to iterate json data in angular2. If JSON Data is like this

{fileName: "XYZ"}

I am able to iterate using- let data of datas

But If my JSON data key is in string format, how I can iterate in angular2?

{"fileName": "XYZ"}
2
  • isn't it same as normal json data? Commented Sep 1, 2016 at 8:13
  • @PriyeshKumar, first one is iterating successfully using let data of datas but second one is not iterating using same Commented Sep 1, 2016 at 8:39

1 Answer 1

1

JSON always have double quoted string keys, so these:

{ fileName: "XYZ" }
{ 'fileName': "XYZ" }

Are not valid jsons, but this is:

{ "fileName": "XYZ" }

Javascript objects don't require the keys to be quoted, and if they are then a single quote can be used:

let a = { fileName: "XYZ" };
let b = { 'fileName': "XYZ" };
let c = { "fileName": "XYZ" };

Here a, b and c are equivalent.

In any case, iterating all of those js object is done in the same way:

for (let key in a) {
    console.log(`${ key }: ${ a[key] }`);
}

Object.keys(b).forEach(key => console.log(`${ key }: ${ b[key] }`));
Sign up to request clarification or add additional context in comments.

Comments

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.