简体   繁体   中英

Passing a key in object as function argument in Javascript

I'm going to write a function in Javascript. It will process by passing a key.

// generate by coffeescript

var get_key, obj;

obj = [
  { name: 'Ape', sales: 100, location: 'US' },
  { name: 'Bob', sales: 200, location: 'UK' },
  { name: 'Cat', sales: 120, location: 'Hell' }
];

// What I want to do: (of cause it is wrong)

get_key = function(obj, key) {
  var item, value_arr, _i, _len;
  value_arr = [];
  for (_i = 0, _len = obj.length; _i < _len; _i++) {
    item = obj[_i];
    value_arr.push(obj.key);
  }
  return value_arr; // return array containing all values of key in object
};

alert(get_key(obj, 'sales')); // expect return 100,200,120

Problem is: how to pass the 'key'?

ps I even don't know any keyword to search in this matter (bad English). So I can't find any answer in StackOverflow and Google. Please give some tips to ask correctly.

Many thanks!!

var get_key, obj;

obj = [
  { name: 'Ape', sales: 100, location: 'US' },
  { name: 'Bob', sales: 200, location: 'UK' },
  { name: 'Cat', sales: 120, location: 'Hell' }
];

// What I want to do: (of cause it is wrong)

get_key = function(obj, key) {
  var item, value_arr, _i, _len;
  value_arr = [];
  for (_i = 0, _len = obj.length; _i < _len; _i++) {
    item = obj[_i];
    if (key in item){
        value_arr.push(item[key]);
    }
  }
  return value_arr; // return array containing all values of key in object
};

alert(get_key(obj, 'sales')); // expect return 100,200,120

item is the object with the key you're looking for so change value_arr.push(obj.key); to value_arr.push(item[key]); , you have to use [] notation to access an objects property via a variable, also added a check to see if the item has a property key before adding to the array.

function get_key(arr, key) {
    return arr.map(function(el) {
        return el[key];
    });
}

If need to support old browsers, you need to add the map function to Array ( check here ).

var obj = [
    {
    name: 'Ape',
    sales: 100,
    location: 'US'},
{
    name: 'Bob',
    sales: 200,
    location: 'UK'},
{
    name: 'Cat',
    sales: 120,
    location: 'Hell'}
];

get_key = function(obj, key) {
    var result = [];
    for (var i = 0; i < obj.length; i++) {
        /*
         *  Here, obj[i] gives the object e.g: 
         * { name: 'Ape', sales: 100, location: 'US' }...
         * 
         * and obj[i][key] give the value of corresponding key e.g:
         * 100, 200... (suppose for: sales) 
         */

        result.push(obj[i][key]);
    }
    return result;
};

alert(get_key(obj, 'sales').join(',')); ​

Working sample


In your code

just change the line

item = obj[_i];
value_arr.push(obj.key);// this line

to

 value_arr.push(item[key]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM