How can I retrieve the object into an array in Javascript? -
i'm practicing basic problem in js , new of these languages. want retrieve value , name of property array "temp" object "second" if obj "third" has same property value. can it, when property name defined, how can same if don't know actual property name. may using object.keys()
my code this:
function where(second, third) { var arr = []; var temp=[]; for(var in second) { if(third.hasownproperty('last')){ if(second[i].last===third.last){ arr=second[i]; temp.push(arr); } } if(third.hasownproperty('first')){ if(second[i].first===third.first){ arr=second[i]; temp.push(arr); } } } return temp; } where([{ first: 'ram', last: 'ktm' }, { first: 'sam', last: null }, { first: 'tybalt', last: 'capulet' }], { last: 'capulet' });
the resulting array : [{ 'first': 'tybalt', 'last': 'capulet' }]
how can retrieve same result if don't know actual property name. instance name here first , last might food, , uses unknown. i've gone following threads here.
[2]: https://stackoverflow.com/questions/4607991/javascript-transform-object-into-array
[1]: https://stackoverflow.com/questions/4607991/javascript-transform-object-into-array
function where(second, third) { var temp = []; var k = object.keys(third)[0]; // expecting 1 element! second.foreach(function (obj) { if (obj[k] === third[k]) { temp.push(obj); } }); return temp; }
Comments
Post a Comment