从对象数组中提取属性值作为数组

objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];
function getFields(input, field) {
    var output = [];
    for (var i=0; i < input.length ; ++i)
        output.push(input[i][field]);
    return output;
}

var result = getFields(objArray, "foo"); // returns [ 1, 3, 5 ]

答案

let result = objArray.map(a => a.foo);
let result = objArray.map(({ foo }) => foo)
var result = objArray.map(function(a) {return a.foo;});
var result = objArray.map(a => a.foo);
var result = _.pluck(objArray, 'foo');
var result = _.map(objArray, 'foo');
var result = _.map(objArray, _.property('foo'));
var objArray = [
    {
        someProperty: { aNumber: 5 }
    },
    {
        someProperty: { aNumber: 2 }
    },
    {
        someProperty: { aNumber: 9 }
    }
];
var result = _.map(objArray, _.property('someProperty.aNumber'));
var result = _.map(objArray, _.property(['someProperty', 'aNumber']));
var result = R.pluck('foo')(objArray);  // or just R.pluck('foo', objArray)
var vals=[];
for(var i=0;i<testArray.length;i++){
   vals.push(testArray[i].val);
}
var vals=[];
for(var item of testArray){
   vals.push(item.val); 
}
var vals = testArray.map(function(a) {return a.val;});
function getFields(input, field) {
    return input.map(function(o) {
        return o[field];
    });
}
_.map(objArray,"foo")
_.pluck(objArray,"foo")
[1, 2, 3]
const objArray = [{foo: 1, bar: 2}, {foo: 3, bar: 4}, {foo: 5, bar: 6}]
objArray.map(({ foo }) => foo)
function getFields(list, field) {
    //  reduce the provided list to an array only containing the requested field
    return list.reduce(function(carry, item) {
        //  check if the item is actually an object and does contain the field
        if (typeof item === 'object' && field in item) {
            carry.push(item[field]);
        }

        //  return the 'carry' (which is the list of matched field values)
        return carry;
    }, []);
}
function getFields(list, field, otherwise) {
    //  reduce the provided list to an array containing either the requested field or the alternative value
    return list.reduce(function(carry, item) {
        //  If item is an object and contains the field, add its value and the value of otherwise if not
        carry.push(typeof item === 'object' && field in item ? item[field] : otherwise);

        //  return the 'carry' (which is the list of matched field values)
        return carry;
    }, []);
}
function getFields(list, field, otherwise) {
    //  map the provided list to an array containing either the requested field or the alternative value
    return list.map(function(item) {
        //  If item is an object and contains the field, add its value and the value of otherwise if not
        return typeof item === 'object' && field in item ? item[field] : otherwise;
    }, []);
}
function getFields(list, field, otherwise) {
    //  determine once whether or not to use the 'otherwise'
    var alt = typeof otherwise !== 'undefined';

    //  reduce the provided list to an array only containing the requested field
    return list.reduce(function(carry, item) {
        //  If item is an object and contains the field, add its value and the value of 'otherwise' if it was provided
        if (typeof item === 'object' && field in item) {
            carry.push(item[field]);
        }
        else if (alt) {
            carry.push(otherwise);
        }

        //  return the 'carry' (which is the list of matched field values)
        return carry;
    }, []);
}
function getFields(list, field, otherwise) {
    var alt = typeof otherwise !== 'undefined';

    return list.reduce(function(carry, item) {
        return carry.concat(typeof item === 'object' && field in item ? item[field] : (alt ? otherwise : []));
    }, []);
}
let a = [{ z: 'word', c: 'again', d: 'some' }, { u: '1', r: '2', i: '3' }];
let b = a.reduce((acc, obj) => [...acc, Object.values(obj).map(y => y)], []);

console.log(b)
for (let i in a) {
  let temp = [];
  for (let j in a[i]) {
    temp.push(a[i][j]);
  }
  array.push(temp);
}

这取决于您对 “更好” 的定义。

其他答案指出地图的使用是自然的(特别是对于习惯使用功能样式的人)并且简洁。我强烈建议使用它(如果您不打扰那些 IE8-IT 专家的话)。因此,如果 “更好” 的意思是 “更简洁”,“可维护”,“可理解”,那么可以,那就更好了。

另一方面,这种美丽并非没有额外费用。我不是 microbench 的忠实拥护者,但是我在这里做了一个小测试 。结果是可以预测的,旧的丑陋方法似乎比 map 函数要快。因此,如果 “更好” 的意思是 “更快”,那么不,保留传统的流行。

同样,这只是一个微平台,绝不主张使用map ,这只是我的两分钱:)。