-2

I am basically looking for a quick way to do this in javascript:

let arr = [{key: "alpha", value: "2", property1: "some"},{key:"beta", value:"3", property1: "extra", property2: "property"},{key: "gamma", value: "4"}];
let obj = someFunction(arr);
// WANTED RESULT:
//obj is now { "alpha" : { value: "2", property1: "some" }, "beta" : {value: "3", property1: "extra", property2: "property",} , "gamma": {value: "4"} }

Each object in the array can have more properties, and different ones, except for key.

Edit: This is what I have done. It still leaves a property on the objects, and I am not sure if this/looping is the most efficient way.

tempObj = {};
for (let i = 0; i < arr.length; i++) {
    tempObj[arr[i].key] = arr[i];
}
//this will give this answer, close enough to what I want: 
//{ alpha: {key: "alpha", value: "2", property1: "some"}, beta: key:"beta", value:"3", property1: "extra", property2: "property", gamma: {key: "gamma", value: "4"} }
amp
  • 109
  • 1
  • 1
  • 9

5 Answers5

1

this would do

let arr = [{key: "alpha", value: "2"},{key:"beta", value:"3"},{key: "gamma", value: "4"}];
var obj = {};
arr.forEach(item => obj[item.key] = item);
console.log(obj);
Stephan
  • 2,028
  • 16
  • 19
Nemani
  • 778
  • 5
  • 12
  • 2
    In such use case `forEach` is better suited than `map`. – Stephan Aug 24 '17 at 11:42
  • Sorry, I just updated my question. Each object in the array is complex and we only have the guarantee that 1 property "key" is same for all. – amp Aug 24 '17 at 11:47
  • @Stephan Could you explain why forEach is better here? – nHaskins Aug 24 '17 at 11:49
  • map returns a new array, which is discarded here. – Nina Scholz Aug 24 '17 at 11:51
  • He used the `map` function just for iterating the array. `forEach` therefore has the same effect while being semantically more clear. Additionally it has a (mostly negligible) performance benefit since it doesn't push the newly created object into a new, unused array. – Stephan Aug 24 '17 at 11:53
  • With the updated question it should be at least `arr.forEach(item => obj[item.key] = item);`. – Stephan Aug 24 '17 at 11:56
0

You can do this by this way

let arr = [{key: "alpha", value: "2", property1: "some"},{key:"beta", value:"3", property1: "extra", property2: "property"},{key: "gamma", value: "4"}];

let result = {};

for(let element of arr){
    result[element.key] = element;
}

console.log(result);
marvel308
  • 10,288
  • 1
  • 21
  • 32
  • Sorry, I just updated my question. Each object in the array is complex and we only have the guarantee that 1 property "key" is same for all. – amp Aug 24 '17 at 11:47
  • I just updated your code with a slightly different array - I can work with the results (my own code gives me the same output) but doesn't reduce it to what i want exactly – amp Aug 24 '17 at 12:02
0

You could use Object.assign for assigning new properties with a new object while iterating.

var array = [{ key: "alpha", value: "2", property1: "some" }, { key: "beta", value: "3", property1: "extra", property2: "property" }, { key: "gamma", value: "4" }],
    object = array.reduce((r, o) =>
        Object.assign(r, {
            [o.key]: Object.keys(o)
                .reduce((s, k) => Object.assign(s, k !== 'key' ? { [k]: o[k] } : {}), {})
            }),
        {});

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Sorry, I just updated my question. Each object in the array is complex and we only have the guarantee that 1 property "key" is same for all. – amp Aug 24 '17 at 11:47
0

If you would like to also remove the abundant key property you could use:

const array = [{key: "alpha", value: "2"}, {key: "beta", value: "3"}, {key: "gamma", value: "4", additional: "sample"}];
const obj = {};
array.forEach(item => {obj[item.key] = item; delete item['key'];});
console.log(obj);
console.log(array);

However note that the original array's entries get modified (the key properties get removed). If that isn't intended you have to (deep?) clone the items:

const array = [{key: "alpha", value: "2"}, {key: "beta", value: "3"}, {key: "gamma", value: "4", additional: "sample"}];

const obj = {};
array.forEach(item => {
  obj[item.key] = Object.assign({}, item);
  delete obj[item.key]['key'];
});
console.log(obj);
console.log(array);

However note the browser support for Object.assign, if you want to support for example IE, you need a polyfill (maybe also for forEach and replace the arrow function).

Stephan
  • 2,028
  • 16
  • 19
0

Thanks everyone. It looks as it will involve some kind of loop and delete operation, which I will avoid using because of performance issues: How do I remove a property from a JavaScript object?

tempObj = {};
for (let i = 0; i < arr.length; i++) {
    tempObj[arr[i].key] = arr[i];
}
//this will give this answer, close enough to what I want: 
//{ alpha: {key: "alpha", value: "2", property1: "some"}, beta: key:"beta", value:"3", property1: "extra", property2: "property", gamma: {key: "gamma", value: "4"} }
amp
  • 109
  • 1
  • 1
  • 9