code K.O. code K.O. - 3 months ago 11
Javascript Question

HasOwnProperty Like Javascript

Problem :
Different Object Name every Output

var data = {'aname':'A','akey':1};

// get object aname
$.each(data, function (i, d) {
console.log(d.aname);
});

var data = {'bname':'A','bkey':1};

// get object bname
$.each(data, function (i, d) {
console.log(d.bname);
});


This is possible to not repetitive that?
i must get the value with bypass prefix with
hasOwnProperty
, so
aname
,
bname
*name
, how could this possible? any javascript
hasOwnProperty
like? such as
hasOwnPropert('%name%');

mmm mmm
Answer

If your object only contains one key with a name containing name, the following will do it

var data = {'bname':'A','bkey':1};

Object.keys(data).forEach(function(key) {
    if (key.indexOf("name") > -1) {
    console.log(data[key]);
  }
});

jQuery:

// get object bname
var data = {'bname':'A','bkey':1};

$.each(data, function (key, value) {
  if (key.indexOf("name") > -1) {
    console.log(value);
  }
});

DEMO