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.

folpy
  • 139
  • 2
  • 10

1 Answers1

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

Community
  • 1
  • 1
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
  • Yes I know but I do not know that there is some value called first. I need to find if there is. – folpy Jan 19 '14 at 14:17
  • 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 Jan 19 '14 at 14:28
  • @folpy I don't really understand what it is you are trying to do. if not undefined tells you if an item exists. – Ashley Medway Jan 19 '14 at 14:32
  • 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 Jan 19 '14 at 14:36
  • @folpy So I've updated my answer again I think this is what you mean. – Ashley Medway Jan 19 '14 at 14:43
  • Thank you very much for your fast help. This is exactly what I need. Again thank you. – folpy Jan 19 '14 at 14:49