Javascript:只选择对象

时间:2017-08-20 17:35:00

标签: javascript

我有我的功能的配置对象:

const features = {
    "feature1": {
        "value": 1,
        "description": ""
    },
    "feature2": {
        "value": 2,
        "description": ""
    }
}

如何将其转换为

等特征/值格式
{"feature1": 1, "feature2": 2}

我的变体如下,但我觉得它不优雅

let config = {};

config = Object.keys(features).map(
    k  => config[k] = flags[k]["value"]
);

1 个答案:

答案 0 :(得分:1)

您可以ES6Object.assign()使用map()点差语法。



const features = {"feature1":{"value":1,"description":""},"feature2":{"value":2,"description":""}}

var result = Object.assign({}, ...Object.keys(features).map(k => ({[k]: features[k].value})))
console.log(result)




或者您可以使用Object.keys()reduce()



const features = {"feature1":{"value":1,"description":""},"feature2":{"value":2,"description":""}}

var result = Object.keys(features).reduce((r, e) => (r[e] = features[e].value, r), {})
console.log(result)