1

I have configuration object for my features:

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

How to transform it to feature/value format like

{"feature1": 1, "feature2": 2}

My variant is below, but I feel it's not elegant

let config = {};

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

1 Answer 1

1

You can use ES6 spread syntax with Object.assign() and 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)

Or you can use Object.keys() and 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)

Sign up to request clarification or add additional context in comments.

1 Comment

Another handy way SelectQL.js is inspired by Structured Query Language (SQL) for accessing and manipulating Objects in an easy and familiar way. It supports complex Objects and Arrays using Builder Design Pattern. npmjs.com/package/selectql.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.