0

I would like to ask you how to filter value from these JSON

{first: "100", second: "200", third: "300"}

I mean that I need something like this

if(json=first){select value}

I wrote this part to explain you what I need. I hope that you understand me.

Thank you for your help.

1
  • 1
    Do you need the value of the first property? yourObject.first? Commented Jan 19, 2014 at 14:09

1 Answer 1

2

So if

var json = {first: "100", second: "200", third: "300"};
var valueFromSlider = "first";

if(json[valueFromSlider] !== undefined) {
    // The valueFromSlider i.e. "first" exists in the json object.
    alert(json[valueFromSlider]); //alerts 100
}

See null/undefined checking here: https://stackoverflow.com/a/858193

I think what you are trying to do should look like this.

var json = {100: "first", 200: "second", 300: "third"};
var valueFromSlider = 100;

if(json[valueFromSlider] !== undefined) {
    // The valueFromSlider i.e. 100 exists in the json object.
    alert(json[valueFromSlider]); //alerts "first"
}


Console output

var json = {first: "100", second: "200", third: "300"};

json.first
"100"

json["first"]
"100"

json.first === "100"
true

json.first !== undefined
true

json.another !== undefined
false

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

6 Comments

Yes I know but I do not know that there is some value called first. I need to find if there is.
Sorry but I think that it is not what I need. I try to explain one more. I have value which is changing we can call it value then I have json object and I need to find if in json object exist this value which is changing and if exist then write this value into some DIV. Do you understand what I want? Thank you for your help
@folpy I don't really understand what it is you are trying to do. if not undefined tells you if an item exists.
I try to explain it from beginning. I have jquery form slider which give some value and then I have this JSON object. What I need is to searching value from jquery form slider in JSON object. Now you understand what I want to do? Again thank you for your help
@folpy So I've updated my answer again I think this is what you mean.
|

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.