我有一个JavaScript对象数组,其结构如下:

objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];

我想从每个对象中提取一个字段,并获得一个包含值的数组,例如,字段foo将给出数组[1,3,5]。

我可以用这种简单的方法做到这一点:

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 ]

是否有更优雅或更惯用的方法来实现这一点,从而不需要自定义实用程序函数?


注意建议的重复,它介绍了如何将单个对象转换为数组。


当前回答

在处理对象数组时,函数映射是一个很好的选择。尽管已经发布了许多好的答案,但结合使用地图和过滤器的示例可能会有所帮助。

如果要排除值未定义的财产或只排除特定属性,可以执行以下操作:

    var obj = {value1: "val1", value2: "val2", Ndb_No: "testing", myVal: undefined};
    var keysFiltered = Object.keys(obj).filter(function(item){return !(item == "Ndb_No" || obj[item] == undefined)});
    var valuesFiltered = keysFiltered.map(function(item) {return obj[item]});

https://jsfiddle.net/ohea7mgk/

其他回答

从对象数组中轻松提取多个财产:

let arrayOfObjects = [
  {id:1, name:'one', desc:'something'},
  {id:2, name:'two', desc:'something else'}
];

//below will extract just the id and name
let result = arrayOfObjects.map(({id, name}) => ({id, name}));

结果将是〔{id:1,名称:'one'},{id:2,名称:'two'}〕

根据需要在map函数中添加或删除财产

虽然map是从对象列表中选择“列”的适当解决方案,但它也有缺点。如果没有明确检查列是否存在,它将抛出一个错误,并(最多)为您提供undefined。我会选择reduce解决方案,它可以简单地忽略该属性,甚至为您设置默认值。

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;
    }, []);
}

jsbin示例

即使提供的列表中的某个项不是对象或不包含该字段,这也会起作用。

如果项不是对象或不包含字段,甚至可以通过协商默认值来使其更加灵活。

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;
    }, []);
}

jsbin示例

这与map相同,因为返回的数组的长度与提供的数组相同。(在这种情况下,地图比缩减略便宜):

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;
    }, []);
}

jsbin示例

还有一个最灵活的解决方案,它可以让你通过提供一个替代价值在两种行为之间切换。

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;
    }, []);
}

jsbin示例

正如上面的示例(希望)揭示了这一工作方式,让我们通过使用Array.concat函数来稍微缩短该函数。

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 : []));
    }, []);
}

jsbin示例

在处理对象数组时,函数映射是一个很好的选择。尽管已经发布了许多好的答案,但结合使用地图和过滤器的示例可能会有所帮助。

如果要排除值未定义的财产或只排除特定属性,可以执行以下操作:

    var obj = {value1: "val1", value2: "val2", Ndb_No: "testing", myVal: undefined};
    var keysFiltered = Object.keys(obj).filter(function(item){return !(item == "Ndb_No" || obj[item] == undefined)});
    var valuesFiltered = keysFiltered.map(function(item) {return obj[item]});

https://jsfiddle.net/ohea7mgk/

上面提供的答案适合于提取单个属性,如果您想从对象数组中提取多个属性,该怎么办。这就是解决方案!!在这种情况下,我们可以简单地使用_.pick(object,[paths])

_.pick(object, [paths])

假设objArray具有如下三个财产的对象

objArray = [ { foo: 1, bar: 2, car:10}, { foo: 3, bar: 4, car:10}, { foo: 5, bar: 6, car:10} ];

现在我们要从每个对象中提取foo和bar属性,并将它们存储在一个单独的数组中。首先,我们将使用map迭代数组元素,然后对其应用Lodash Library Standard_.pick()方法。

现在我们可以提取“foo”和“bar”属性。

var newArray=objArray.map((element)=>{return _.pick(element,['fo','bar'])})console.log(newArray);

结果是〔{foo:1,bar:2},{foo:3,bar:4},{foo:5,bar:6}〕

享受

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

其他答案指出了地图的使用,这是自然的(尤其是对于习惯于功能风格的人),而且简洁。我强烈建议您使用它(如果您不介意使用少数IE8-it人员)。所以,如果“更好”意味着“更简洁”、“可维护”和“可理解”,那么是的,它会更好。

另一方面,这种美丽不是没有额外成本的。我不太喜欢微面包,但我在这里做了一个小测试。结果是可以预测的,旧的丑陋的方式似乎比地图功能更快。所以,如果“更好”意味着“更快”,那么就不要,继续沿用老式的时尚。

同样,这只是一个微框架,绝不反对使用地图,这只是我的两分钱:)。