0

I am receiving a json response from an API call. I need to store its keys, and create an array of an object. I am intending to this array of an object is created dynamically no matter the keys of the response.

I've already got the keys like this:

  const json_getAllKeys = data => {
   const keys = data.reduce((keys, obj) => (
      keys.concat(Object.keys(obj).filter(key => (
        keys.indexOf(key) === -1))
      )
    ), [])
    return keys 
}

That returned an array (using a sample json):

['name','username', 'email']

But I am trying to use that array to create an array of object that looks like this one

[
    {
      name: "name",
      username: "username",
      email: "Email",
    }
];

I've been trying mapping the array, but got multiple objects because of the loop, and I need a single one to make it work.

keys.map(i=>({i:i}))

[
  { i: 'id' },
  { i: 'name' },
  { i: 'username' },
  { i: 'email' }
]

Any hint would be useful!

Thanks in advance :D

10
  • yes exactly, no way to do that? Commented Jan 26, 2021 at 2:06
  • there is many, but I don't understand how is your original data Commented Jan 26, 2021 at 2:07
  • original data comes from a json. From there I got an array with the keys of that response! Commented Jan 26, 2021 at 2:10
  • you mean data = ['name','username', 'email'] ? or keys = ['name','username', 'email'] (returned and in this case what is the value of data) ? this is unclear Commented Jan 26, 2021 at 2:16
  • your code on data.reduce((keys, obj) is wrong, Array.reduce() work in an other way, it use accumulator Commented Jan 26, 2021 at 2:25

2 Answers 2

1

What you're looking for is Object.fromEntries, which is ECMA2019, I believe, so available in Node >=14 and will be provided as a polyfill if you employ babel.

I can't quite discern what your reduce should produce, but given the sample input, I would write

const input = ['name','username', 'email'];

const result = Object.fromEntries(input.map(name => ([name, name])));
// result == { name: 'name', username: 'username', email: 'email' }
Sign up to request clarification or add additional context in comments.

Comments

1

You're definitely on the right track. One thing to remember is the map function will return the SAME number of output as input. So in your example, an array of 3 returns an array of 3 items.

For this reason, map alone is not going to give you what you want. You may be able to map => reduce it. However, here is a way using forEach instead. This isn't a strictly functional programming style solution, but is pretty straight forward and depending on use case, probably good enough.

let keys = ['name','username', 'email'] //you have this array
const obj = {}; // empty object to hold result
keys.forEach(i => {
    obj[i] = i;  // set the object as you want
})

console.log(obj); // log out the mutated object
// { name: 'name', username: 'username', email: 'email' }

1 Comment

Leaving this here because it is a solution. Overall though I think John's answer is 'better'.

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.