0

My List is a list of dictionaries and i want to add another dictionary to it. But the output displays the items in the list inside each dictionary entry as [array].

dict={ 
 "Computer": [{"file" : "MyDirectory/A/text1.txt", "line" :[23,56]},
             {"file" :"MyDirectory/B/text5.txt", "line" :[32,91]}]
}

word="Computer"
key='123'
src="MyDirectory/C/text9.txt"

dict[word]=dict[word].concat({"file" : src, "line" : [key]})
console.log(dict)

This is the output

How can I make the output look like this:

{ "Computer":
    [ { "file":"MyDirectory/A/text1.txt", "line":[23,56]},
      { "file":"MyDirectory/B/text5.txt", "line":[32,91]},
      { "file":"MyDirectory/C/text9.txt", "line":[123]}]}
2
  • what do you want the output to be? – Commented Mar 16, 2018 at 18:09
  • "Computer": [{"file" : "MyDirectory/A/text1.txt", "line" :[23,56]}, {"file" :"MyDirectory/B/text5.txt", "line" :[32,91]}, {"file" : "MyDirectory/C/text9.txt", "line" :[123]}] } instead of the list of integers after "line" : , it just displays [Array] right now Commented Mar 16, 2018 at 18:13

1 Answer 1

2

console.log() will only display your items to a certain depth. If you just need a pretty output try:

console.log(JSON.stringify(dict, null, 2));
Sign up to request clarification or add additional context in comments.

3 Comments

this takes up too many lines. Any way to output in the exact way i've described in the question?
AFAIK no, the "pretty formatting" of JSON.stringify() is fairly limited to it's purpose. If that isn't sufficient, you propably need to write your own output function, which produces the output string.
@HamzaAhmad Replacing spaces/newlines will make it "compact", console.log(JSON.stringify(dict, null, 2).replace(/[ \n]/g,'')). This still seems like an X/Y problem. A debugger makes viewing variables much easier.

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.