Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am having an JOSN object {"Display":{"FIRST_NAME":"X","MID_NAME":"X","LAST_NAME":"X","LAST_CHANGED":"X","ADP_FILE":"NA","MARITAL":"NA","EXEMP":"NA"}}

I want only the fields having value "X" form above JSON object.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Apr-15 12:23pm    
What exactly do you mean by "I want"? What have you tried so far?
—SA

1 solution

This "want only" in not a specification of what you need. What would it mean? Undefine part of the properties? Create a new JavaScript object (not string) with the same properties except those with the values other then "X"? You can do it. What?

Anyway, you can use:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^],
create a structure from JSON string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse[^],
traverse any object in a loop: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in[^],
and this is how you can remove a property from an object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete[^].

You can create a new object and create new properties in a loop;
JavaScript
var result = {};
// ...
result.LAST_NAME = "X"; //sic!


JavaScript objects (including arrays) are associative containers; you can add any property at any time, and the property lookup has time complexity of O(1):
http://en.wikipedia.org/wiki/Associative_array[^],
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^];
see also: http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

—SA
 
Share this answer
 
Comments
vickyramsey 24-Apr-15 2:20am    
Actually i want to select the fields which are having value "X".
Anyway thanks for your help
Sergey Alexandrovich Kryukov 24-Apr-15 2:42am    
You did not answer my question: what does it mean, "select"?

At the same time, I essentially answered yours, no matter what are the detail of your requirements.
So, are you going to accept it formally?
In all cases, your follow-up questions will be welcome.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900