2

I want to form an object based the keys from another object and set values to empty string;

const availableList = [
  {id: 1, key: 'emp_id'},
  {id: 2, key: 'firstname'},
  {id: 3, key: 'lastname'},
  {id: 4, key: 'mobile'},
  {id: 5, key: 'email'},
]
    
availableList .forEach(key => result [key] = availableList [key]);

Expected output:

result = {
  emp_id: '', 
  firstname: '', 
  lastname: '', 
  mobile: '', 
  email: ''
}
2
  • Please provide a clear question. A question should always contain a question mark ;) Commented May 31, 2021 at 7:20
  • i have provided the available list expected output and what i have tried :) Commented May 31, 2021 at 7:22

4 Answers 4

3

You could use .reduce with object destructing and object's computed property

const availableList = [
  { id: 1, key: "emp_id" },
  { id: 2, key: "firstname" },
  { id: 3, key: "lastname" },
  { id: 4, key: "mobile" },
  { id: 5, key: "email" },
]

const res = availableList.reduce(
  (finalObj, iteratedObj) => ({ ...finalObj, [iteratedObj.key]: "" }),
  {}
)

console.log(res)

References

Array.property.reduce

Spread syntax

Destructing assignment

Computed property names (ES 2015)

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

Comments

2

If you're not very familiar with javascript, arrays and objects, it's probably better to keep it simple (avoid arrow functions and fancy es6 notation).

A simple Array#reduce should do the trick:

const availableList = [
    {id: 1, key: 'emp_id'},
    {id: 2, key: 'firstname'},
    {id: 3, key: 'lastname'},
    {id: 4, key: 'mobile'},
    {id: 5, key: 'email'},
];

var newObject = availableList.reduce(function (result, item) {
  result[item.key] = '';
  return result;
}, {});

console.log(newObject);

Some comments in case you're familiar with Array#reduce:

  • The parameter I've named result is the value you use to "reduce" the array into a single variable.
  • The parameter I've named item is the value of item of array during each iteration.
  • The original value of result is the value specified as the second parameter of reduce (which in this case is {})
  • Inside the function passed to the reduce, you have to return the value that will be passed in as the result parameter in the next iteration
  • So in each iteration, you get the object with they keys you've added up to that point, you add another key with a value of empty string and then return it so that it can be used for the next iteration.
  • Once it's done iterating, it will return the last value, that was returned by the inside function. That value gets assigned to the variable newObject

Comments

2

Here's my take on it using Object.fromEntries():

const availableList = [
  { id: 1, key: "emp_id" },
  { id: 2, key: "firstname" },
  { id: 3, key: "lastname" },
  { id: 4, key: "mobile" },
  { id: 5, key: "email" },
];

const result = Object.fromEntries(availableList.map((item) => [item.key, ""]))

console.log(result);

Comments

1

You have an array of properties. So as you suggested in your question, you need to use a forEach to iterate all the items. The items are objects in this case. So you can access all their properties just writing obj.property.

Let's see how can I add a property to an object. I could do:

obj.property5 = 'something'

Or I could do:

obj['property5'] = 'something'

Since you have something unpredictable as the name of the property you should use the second one. Here there's the code:

var secondObject = {};

availableList.forEach(function(item, index) {
  secondObject[item.key] = '';
});

2 Comments

Pretty good answer, but if the goal is to keep it simple, then shouldn't you remove the index parameter from the Array#forEach callback?
Yeah of course. I left it there because if someone doesn't know how it works he/she can understand 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.