0

I am trying to store the values inside a javascript object notation based on the field selected by user. I was able to store the value in to a string separated with commas, Bt if the term Im trying to find is an array, I get [object object] in return, as it is an array. I want to know how I can store all the items in that array in to a variable separated by commas, here for example if I select "Time", then it should return Dec 9, 1, 2012

In the example, there is a function findProps, If I give the argument, entityCount inside findProps("entityCount", data), I will get a proper return type as 50, bsed on the json Ive provided. Bt If I add, findProps("Time", data);, since it is an array, it returns [object, object], not the values inside those arrays and I want the all values inside that array to be displayed(like Dec 9, 1, 2012) and the keys inside each array could be different and the depth of array could also be different. Im just using a static example here.

The part Ive reached returns the value if its a non array, http://jsbin.com/obehog/3/edit

There is another example which I came close to doing, http://jsbin.com/obehog/4/edit bt Im bad at recursion so Im stuck at this..

and the depth of the arrays could change, it wont be the same in each case. so going through loop will not work..

4
  • I don't understand what you're trying to do. Could you perhaps update your question with a clear example input and corresponding (desired) output? Commented Jul 10, 2012 at 6:41
  • Me neither - and looking at your JSBin, it does output ".... Time, Dec 9, 2012..." Commented Jul 10, 2012 at 6:46
  • @Jeff check out jsbin.com/obehog/4/edit#preview and you will understand what I mean.. Commented Jul 10, 2012 at 6:49
  • You can use for (prop in obj) { obj[prop]; } to access all properties, including all entries in an array and add them to a string. I wouldn't do this as anything more than debugging however as you don't really want to do this on complex objects (functions and everything would get printed out by this). Commented Jul 10, 2012 at 6:52

2 Answers 2

1

If I understand you right, you code needs additional function, which will write all object's properties to array. Look at my solution: http://jsbin.com/obehog/6/edit#source

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

1 Comment

Is it possible to order them like placing each set of elemnts in next line.. like for example if we give News in our example it shows everything in single line... and the key value associated with each of them on top once..so that the user can identify which field is being displayed..
0

if i've been understanding your desire, than you want to "seralize" an object (containing strings, arrays and other objects) into a string. therefor you just need to iterate through your object and seralize the parts of it.

quick, dirty and recursive: http://jsfiddle.net/AwhfV/1/

just alter it for your needs

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.