1

I am trying to determine if a key name is present in my array of object. As an example, how would I verify if the key with a name of 'name' equals the argument passed in my function call? Below is code to further clarify.

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
var test = function(arr, propName){
var result = [];
for(var i = 0; i < arr.length; i++){
    if(arr[i][propName] === propName){
        result.push(arr[i][propName]);
    }
}
return result;
} 
func(stooges, "name"); 
Chris Smith
  • 399
  • 3
  • 16
  • What do you expect the `test(stooges, 'name')` to return? – Malk Apr 28 '15 at 23:52
  • 2
    If you only want to test the presence of the property, you can do `if (propName in arr[i])` or `if (arr[i].hasOwnProperty(propName)`. Is that what you want? `arr[i][propName] === propName` compares the **value** of the property with its name, which would only be true if you had `name: "name"` (which would be odd). – Felix Kling Apr 28 '15 at 23:52
  • @Malk I expect test(stooges, "name") to return => ["moe", "larry", "curly"] – Chris Smith Apr 28 '15 at 23:57
  • @TitoEsteves that is explicitly _not_ what your question text asks for. – Alnitak Apr 28 '15 at 23:57
  • http://underscorejs.org/#pluck – Malk Apr 28 '15 at 23:58
  • @FelixKling, Your suggestions worked. Thank you – Chris Smith Apr 28 '15 at 23:59
  • @Malk. Yes I am trying to write my own version of the underscore pluck function. I feel it is good practicing. Thanks – Chris Smith Apr 29 '15 at 00:01
  • @FelixKling your chosen duplicate isn't for the question the OP actually intended to ask. – Alnitak Apr 29 '15 at 00:06
  • @Alnitak: I think it is, otherwise they wouldn't find my comment helpful. What question do you think they wanted to ask? – Felix Kling Apr 29 '15 at 00:07
  • @FelixKling the OP wanted to know how to extract the named property from an array of objects (c.f. `_.pluck`). The latter half of your comment addresses that. – Alnitak Apr 29 '15 at 00:08
  • @Alnitak: But they already seem to know that: `result.push(arr[i][propName]);`. The problem is the test. I agree that the overall problem is to write something like `pluck`, but the question seems to be about testing the existence of the property. – Felix Kling Apr 29 '15 at 00:09

1 Answers1

0

Using underscore.js:

var names = _.pluck(stooges, 'name');

In fact this is the very example given on their page?!

So, on the basis that you knew this, but want to know how to write something similar:

function pluck(array, prop]) {
    return array.map(function(entry) {
        return entry[prop];
    });
}

or more safely, returning an empty array if the initial array is undefined:

function pluck(array, prop]) {
    return array ? array.map(function(entry) {
        return entry[prop];
    }) : [];
}
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Yes I am trying to write my own version of the underscore pluck function. I feel it is good practicing. Thanks – Chris Smith Apr 29 '15 at 00:02
  • 1
    @TitoEsteves I've included that too. You could, of course, have just read the source code for underscore... – Alnitak Apr 29 '15 at 00:02
  • I did not comprehend that. That is far more advanced then the solution I came up with. Still new to programming. – Chris Smith Apr 29 '15 at 00:08