javascript - Can I connect string with Json attribute like this -


i have json data this.

data = {     "purchaseid": 1,     "name": "rubbick",     "date": "2560-06-29",     "price": 12345.00,   },   {     "purchaseid": 2,     "name": "johny",     "date": "2560-07-14",     "work": "navy",     "price": 15000.00   } 

if need element attribute names json use object.keys(data[0]) and

i (4) ["purchaseid", "name", "date", "price"]

so when used object.keys(data[0])[1] result name

then keep var nameofatt = object.keys(data[0])[1]

and need use in

var results = []; var keyword = "johny"; for(var i=0; < data.length; i++){     for(var j=0; j < object.keys(data[i]).length; j++){         if( data[i].object.keys(data[i])[j] == keyword){            results.push(data[i]);         }     } } 

so data object have johny

my question can't use data[i].object.keys(data[i])[j] showed unidentified can use data[i].name

how can connect string this.

once fix data, here's quick es6 example show how filter out objects want data.

const getobjfromkey = (data, key, value) => {   return data.filter((obj) => obj[key] === value); }  const result = getobjfromkey(data, 'name', 'johny'); 

result

[   {     "purchaseid": 2,     "name": "johny",     "date": "2560-07-14",     "work": "navy",     "price": 15000   } ] 

demo


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -