0

I'm currently practicing some basic problem in js and fairly new to any of these languages. I want to retrieve the value and name of the property into an array "temp" from the object "second" if the another obj "third" has the same property value. I can do it, when the property name is already defined, but how can I do the same if I don't know the actual property name. May be using Object.keys()

My code is something like this:

      function where(second, third) {
  var arr = [];
  var temp=[];
  for(var i 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 is : [{ 'first': 'Tybalt', 'last': 'Capulet' }]

How can I retrieve the same result even if I don't know the actual property name. For instance the name here first and last might be food, and uses which is unknown. I've already gone with the 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

Community
  • 1
  • 1
user2906838
  • 1,178
  • 9
  • 20

3 Answers3

1

I think the getOwnPropertyName is what you're looking for. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames

Object.getOwnPropertyNames({a:1,2:"b",c:"a"}).sort() // returns Array [ "2", "a", "c" ]

I'll expand on my initial answer

var temp = [];

var myobj = [{cat:"Felix",dog:"Milo",bird:"Monkey",3:"pets"}, 
         {cat:"Wiskers",dog:"yapper",bird:"tweeter",1:"human"}];

var myobj2 = {cat:"Harry",dog:"Fuzz",1:"human"};

var objKeys = Object.getOwnPropertyNames(myobj); //will have key "1" that matches below

objKeys.forEach(function(key) {
    if(myobj2.hasOwnProperty(key)) {
        temp.push(myobj2[key]) // will push "human" to temp array
    }
})
Knight Yoshi
  • 924
  • 3
  • 11
  • 32
1
function where(second, third) {
    var temp = [];
    var k = Object.keys(third)[0]; // expecting only one element!
    second.forEach(function (obj) {
        if (obj[k] === third[k]) {
            temp.push(obj);
        }
    });
    return temp;
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
function where(second, third) {
  var tmp = [];
  var length = second.length;

  var i, k, test;
  // Go through second's each item to see that...
  for (i = 0; i < length; ++i) {
    test = second[i];
    // For all key in third that belongs to third (not inherited or else)
    for (k in third) {
      // Check has own property.
      if (!third.hasOwnProperty(k)) {
        continue;
      }
      // If second[i] don't have key `k` in second or second[i][k]'s value is 
      // not equal to third[k], then don't put it into tmp, start to check next.
      if (!third.hasOwnProperty(k)) {
        break;
      } else if (test[k] !== third[k]) {
        break;
      }
      tmp.push(test);
    }
  }
  return tmp;
}
fuyushimoya
  • 9,715
  • 3
  • 27
  • 34
  • @fuyshimoya , thank you! I couldn't even vote up. You perfectly filtered the value of the "third". – user2906838 Jun 20 '15 at 08:36
  • I edited, for you use the `hasOwnProperty`, which you may concern if your object is extended or created by some extended prototypes. – fuyushimoya Jun 20 '15 at 08:38
  • What would be the approach, If I wish to use Object.keys(). Assume I'm very newbie. – user2906838 Jun 20 '15 at 08:41
  • @user2906838, see my answer. – Nina Scholz Jun 20 '15 at 08:42
  • @fuyushimoya, ya `hasOwnProperty` seems not a good idea, actually I didn't know how else I could go. So I just thought using it should be in very lower level to understand, actually your first answer was more understandable but this too is fairly simple. Thank you for clarifying me. – user2906838 Jun 20 '15 at 09:37
  • I'm not sure why you think `hasOwnProperty ` is not a good idea, it's good or not depends on how you create the object you want to test, anyway, if you accept Nina Scholz' s answer, notify her that her answer will only check first key of the object, so if there's more than 1 key in `third`, outcome might not be what you want. – fuyushimoya Jun 20 '15 at 09:47
  • Like `where([{ first: 'Ram', last: 'Ktm' }, { first: 'Sam', last: null }, { first: 'Tybalt', last: 'Capulet' }], { first: 'Sam', last: 'Capulet' });` will output `[{first: 'Sam', last: null}]`, which I believe you may expect an empty array? – fuyushimoya Jun 20 '15 at 09:48
  • @fuyushimoya, if you change the specifications, like not null/undefined results, or third is an array, then the answer may vary. – Nina Scholz Jun 20 '15 at 10:00
  • @NinaScholz of course, so I just think it should be better if you edit your code to accept multiple keys situation, as the op didn't mention there's only 1 key in the third object. Anyway, it just a suggestion, not a request or something like that. – fuyushimoya Jun 20 '15 at 10:08