1

What would be a syntactically optimal way to obtain a JS object from JSON? I have a json file describing form fields, from it I would like to obtain JS object that contains only the value of the name field, equal to null. Like so:

JSON Input :

[
    {
        "name": "firstname",
        "en": "First Name",
        "fr": "Prénom",
        "type": "text"
    },
    {
        "name": "name",
        "en": "Last Name",
        "fr": "Nom",
        "type": "text"
    },
    {
        "name": "email",
        "en": "Email",
        "fr": "Email",
        "type": "text"
    },
    {
        "name": "password",
        "en": "Password",
        "fr": "Mot de Passe",
        "type": "password"
    }
]

Js object result:

{
    firstname: null,
    name: null,
    email: null,
    password: null
}

Any suggestions?

2
  • Define "syntactically optimal" - less code? most readable? easier to understand and reuse? Commented Jan 11, 2018 at 11:51
  • Less code yes, I need to get this done in one line due to some module I am using Commented Jan 11, 2018 at 11:53

3 Answers 3

2

You could use Object.assign with spread syntax ... for the properties.

var array = [{ name: "firstname", en: "First Name", fr: "Prénom", type: "text" }, { name: "name", en: "Last Name", fr: "Nom", type: "text" }, { name: "email", en: "Email", fr: "Email", type: "text" }, { name: "password", en: "Password", fr: "Mot de Passe", type: "password" }],
    object = Object.assign(...array.map(({ name }) => ({ [name]: null })));
    
console.log(object);

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

Comments

1

Use reduce

var output = arr.reduce( ( a, c ) => ( a[c.name] = null, a ),  {} );

Demo

var arr = [{
    "name": "firstname",
    "en": "First Name",
    "fr": "Prénom",
    "type": "text"
  },
  {
    "name": "name",
    "en": "Last Name",
    "fr": "Nom",
    "type": "text"
  },
  {
    "name": "email",
    "en": "Email",
    "fr": "Email",
    "type": "text"
  },
  {
    "name": "password",
    "en": "Password",
    "fr": "Mot de Passe",
    "type": "password"
  }
]

var output = arr.reduce((a, c) => (a[c.name] = null, a), {});
console.log(output);

2 Comments

I couldn't get this to work, getting error: "Arrow function should not return assignment" error... I believe it is from eslint
Yes it did, but ran into some problems implementing it into my code because of eslint (github.com/eslint/eslint/issues/5150). Nina Scholz solutions was thus better suited.
1

Use array#reduce.

var data = [{ "name": "firstname", "en": "First Name", "fr": "Prénom", "type":"text" }, { "name": "name", "en": "Last Name", "fr": "Nom", "type":"text" }, { "name": "email", "en": "Email", "fr": "Email", "type": "text" }, { "name": "password", "en": "Password", "fr":"Mot de Passe", "type":"password" } ],
    result = data.reduce((r, {name}) => (r[name] = null, r), {});
console.log(result);

Comments

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.