How do I convert array of Objects into one Object in JavaScript?
I have an array of Objects:
[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]
How do I convert it into the following by JavaScript?
{"11":"1000", "22":"2200"}
javascript
|
show 3 more comments
I have an array of Objects:
[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]
How do I convert it into the following by JavaScript?
{"11":"1000", "22":"2200"}
javascript
what language you wanna use in order to convert it?
– silicakes
Nov 9 '13 at 9:47
@Mike86 I want to use JavaScript
– Vijay Chouhan
Nov 9 '13 at 9:48
Do you accept the above as a string?
– silicakes
Nov 9 '13 at 9:53
By the way, those are Objects
– Alexander
Nov 9 '13 at 9:55
2
[{key:"11", value:"1100"}, {key:"22", value:"2200"}].reduce(function(m,v){m[v.key] = v.value; return m;}, {})
– david
Nov 9 '13 at 9:57
|
show 3 more comments
I have an array of Objects:
[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]
How do I convert it into the following by JavaScript?
{"11":"1000", "22":"2200"}
javascript
I have an array of Objects:
[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]
How do I convert it into the following by JavaScript?
{"11":"1000", "22":"2200"}
javascript
javascript
edited Mar 13 '18 at 3:38
Pang
6,9011664101
6,9011664101
asked Nov 9 '13 at 9:44
Vijay ChouhanVijay Chouhan
1,86331932
1,86331932
what language you wanna use in order to convert it?
– silicakes
Nov 9 '13 at 9:47
@Mike86 I want to use JavaScript
– Vijay Chouhan
Nov 9 '13 at 9:48
Do you accept the above as a string?
– silicakes
Nov 9 '13 at 9:53
By the way, those are Objects
– Alexander
Nov 9 '13 at 9:55
2
[{key:"11", value:"1100"}, {key:"22", value:"2200"}].reduce(function(m,v){m[v.key] = v.value; return m;}, {})
– david
Nov 9 '13 at 9:57
|
show 3 more comments
what language you wanna use in order to convert it?
– silicakes
Nov 9 '13 at 9:47
@Mike86 I want to use JavaScript
– Vijay Chouhan
Nov 9 '13 at 9:48
Do you accept the above as a string?
– silicakes
Nov 9 '13 at 9:53
By the way, those are Objects
– Alexander
Nov 9 '13 at 9:55
2
[{key:"11", value:"1100"}, {key:"22", value:"2200"}].reduce(function(m,v){m[v.key] = v.value; return m;}, {})
– david
Nov 9 '13 at 9:57
what language you wanna use in order to convert it?
– silicakes
Nov 9 '13 at 9:47
what language you wanna use in order to convert it?
– silicakes
Nov 9 '13 at 9:47
@Mike86 I want to use JavaScript
– Vijay Chouhan
Nov 9 '13 at 9:48
@Mike86 I want to use JavaScript
– Vijay Chouhan
Nov 9 '13 at 9:48
Do you accept the above as a string?
– silicakes
Nov 9 '13 at 9:53
Do you accept the above as a string?
– silicakes
Nov 9 '13 at 9:53
By the way, those are Objects
– Alexander
Nov 9 '13 at 9:55
By the way, those are Objects
– Alexander
Nov 9 '13 at 9:55
2
2
[{key:"11", value:"1100"}, {key:"22", value:"2200"}].reduce(function(m,v){m[v.key] = v.value; return m;}, {})– david
Nov 9 '13 at 9:57
[{key:"11", value:"1100"}, {key:"22", value:"2200"}].reduce(function(m,v){m[v.key] = v.value; return m;}, {})– david
Nov 9 '13 at 9:57
|
show 3 more comments
11 Answers
11
active
oldest
votes
You're probably looking for something like this:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
add a comment |
I like the functional approach to achieve this task:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {} is the initial obj value for reduce function, if you won't provide the initial value the first arr element will be used (which is probably undesirable).
https://jsfiddle.net/GreQ/2xa078da/
1
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
1
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
add a comment |
Tiny ES6 solution can look like:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj each time, rather assigning new property to it.
1
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
Loved the one!var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
– Sohail
Aug 21 '18 at 13:36
add a comment |
Trying to fix this answer, this should do it:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
add a comment |
A clean way to do this using modern JavaScript is as follows:
const array = [
{ name: "something", value: "something" },
{ name: "somethingElse", value: "something else" },
];
const newObject = Object.assign({}, ...array.map(item => ({ [item.name]: item.value })));
// >> { something: "something", somethingElse: "something else" }
add a comment |
Use lodash!
const obj = _.keyBy(arrayOfObjects, 'keyName')
add a comment |
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
add a comment |
Here you go:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
1
Because that's how you solve this problem. About the naming for example:arrayis a reserved keyword so people usearrinstead. etc.
– pstadler
Nov 9 '13 at 10:36
add a comment |
Here's how to dynamically accept the above as a string and interpolate it into an object:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
http://jsfiddle.net/3QKmX/1/
add a comment |
// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
add a comment |
var obj = [
{id: 1 , name : 'gaju' , class : 'kv'},
{id: 2 , name : 'tushar' , class : 'Super'}
];
function conv(obj,arg){
var temp= {};
var output= {}
for(key in obj){
temp[key] = obj[key];
};
var i=0;
for(key in temp){
var aa = {};
var value = temp[key][arg];
aa[value] = temp[i];
//console.log(aa[value][arg] );
output[aa[value][arg]]= aa[value];
i++;
}
console.log(output);
}
calling function
conv(obj,'class');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19874555%2fhow-do-i-convert-array-of-objects-into-one-object-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're probably looking for something like this:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
add a comment |
You're probably looking for something like this:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
add a comment |
You're probably looking for something like this:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
You're probably looking for something like this:
// original
var arr = [
{key : '11', value : '1100', $$hashKey : '00X' },
{key : '22', value : '2200', $$hashKey : '018' }
];
//convert
var result = {};
for (var i=0; i<arr.length; i++) {
result[arr[i].key] = arr[i].value;
}
//result
console.log(result);
answered Nov 9 '13 at 9:55
TibosTibos
23.3k23456
23.3k23456
add a comment |
add a comment |
I like the functional approach to achieve this task:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {} is the initial obj value for reduce function, if you won't provide the initial value the first arr element will be used (which is probably undesirable).
https://jsfiddle.net/GreQ/2xa078da/
1
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
1
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
add a comment |
I like the functional approach to achieve this task:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {} is the initial obj value for reduce function, if you won't provide the initial value the first arr element will be used (which is probably undesirable).
https://jsfiddle.net/GreQ/2xa078da/
1
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
1
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
add a comment |
I like the functional approach to achieve this task:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {} is the initial obj value for reduce function, if you won't provide the initial value the first arr element will be used (which is probably undesirable).
https://jsfiddle.net/GreQ/2xa078da/
I like the functional approach to achieve this task:
var arr = [{ key:"11", value:"1100" }, { key:"22", value:"2200" }];
var result = arr.reduce(function(obj,item){
obj[item.key] = item.value;
return obj;
}, {});
Note: Last {} is the initial obj value for reduce function, if you won't provide the initial value the first arr element will be used (which is probably undesirable).
https://jsfiddle.net/GreQ/2xa078da/
edited Jun 29 '17 at 12:40
jmarceli
10.9k43943
10.9k43943
answered May 13 '16 at 17:10
GreQGreQ
561165
561165
1
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
1
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
add a comment |
1
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
1
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
1
1
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
and if the key is dynamic?
– Stefano Saitta
Jun 2 '16 at 10:45
1
1
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Well, you should know which property of the given item/object should be used as key and which as value no? But if we assume that wfirst prop is always the key and second the value we could use a callback function like this: function(obj,item){ var keys = item.keys(); obj[item[keys[0]]] = item[keys[0]]; return obj; }
– GreQ
Jun 24 '16 at 7:33
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
Correction of callback above: code var obj = arr.reduce(function(obj,item){ var keys = Object.keys(item); obj[item[keys[0]]] = item[keys[1]]; return obj; },{});
– GreQ
Jun 24 '16 at 7:48
add a comment |
Tiny ES6 solution can look like:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj each time, rather assigning new property to it.
1
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
Loved the one!var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
– Sohail
Aug 21 '18 at 13:36
add a comment |
Tiny ES6 solution can look like:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj each time, rather assigning new property to it.
1
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
Loved the one!var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
– Sohail
Aug 21 '18 at 13:36
add a comment |
Tiny ES6 solution can look like:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj each time, rather assigning new property to it.
Tiny ES6 solution can look like:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
(obj, item) => Object.assign(obj, {[item.key]: item.value}) ,{});
Also, if you use object spread, than it can look like:
var object = arr.reduce((obj, item) => ({...obj, {[item.key]: item.value}}) ,{});
One more solution that is 99% faster is(tested on jsperf):
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
Here we benefit from comma operator, it evaluates all expression before comma and returns a last one(after last comma). So we don't copy obj each time, rather assigning new property to it.
edited Aug 7 '17 at 14:16
answered Jun 2 '17 at 9:14
Shevchenko ViktorShevchenko Viktor
889615
889615
1
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
Loved the one!var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
– Sohail
Aug 21 '18 at 13:36
add a comment |
1
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
Loved the one!var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
– Sohail
Aug 21 '18 at 13:36
1
1
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
The simplest answer of all, yet the most elegant too
– Jeremy Belolo
Nov 9 '17 at 18:49
Loved the one!
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});– Sohail
Aug 21 '18 at 13:36
Loved the one!
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});– Sohail
Aug 21 '18 at 13:36
add a comment |
Trying to fix this answer, this should do it:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
add a comment |
Trying to fix this answer, this should do it:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
add a comment |
Trying to fix this answer, this should do it:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
Trying to fix this answer, this should do it:
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );One-liner:
var newObj = Object.assign({}, ...(array.map(item => ({ [item.key]: item.value }) )));
var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );var array = [
{key:'k1',value:'v1'},
{key:'k2',value:'v2'},
{key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );edited Mar 13 '18 at 3:14
answered Mar 13 '18 at 3:02
FirstOneFirstOne
3,97341533
3,97341533
add a comment |
add a comment |
A clean way to do this using modern JavaScript is as follows:
const array = [
{ name: "something", value: "something" },
{ name: "somethingElse", value: "something else" },
];
const newObject = Object.assign({}, ...array.map(item => ({ [item.name]: item.value })));
// >> { something: "something", somethingElse: "something else" }
add a comment |
A clean way to do this using modern JavaScript is as follows:
const array = [
{ name: "something", value: "something" },
{ name: "somethingElse", value: "something else" },
];
const newObject = Object.assign({}, ...array.map(item => ({ [item.name]: item.value })));
// >> { something: "something", somethingElse: "something else" }
add a comment |
A clean way to do this using modern JavaScript is as follows:
const array = [
{ name: "something", value: "something" },
{ name: "somethingElse", value: "something else" },
];
const newObject = Object.assign({}, ...array.map(item => ({ [item.name]: item.value })));
// >> { something: "something", somethingElse: "something else" }
A clean way to do this using modern JavaScript is as follows:
const array = [
{ name: "something", value: "something" },
{ name: "somethingElse", value: "something else" },
];
const newObject = Object.assign({}, ...array.map(item => ({ [item.name]: item.value })));
// >> { something: "something", somethingElse: "something else" }
answered Oct 9 '18 at 11:11
Hedley SmithHedley Smith
47166
47166
add a comment |
add a comment |
Use lodash!
const obj = _.keyBy(arrayOfObjects, 'keyName')
add a comment |
Use lodash!
const obj = _.keyBy(arrayOfObjects, 'keyName')
add a comment |
Use lodash!
const obj = _.keyBy(arrayOfObjects, 'keyName')
Use lodash!
const obj = _.keyBy(arrayOfObjects, 'keyName')
answered Jun 5 '16 at 13:16
ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhahhhhhhhhhhhhhhhhhhhhhhhhhhhhh
750179
750179
add a comment |
add a comment |
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
add a comment |
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
add a comment |
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
Using Underscore.js:
var myArray = [
Object { key="11", value="1100", $$hashKey="00X"},
Object { key="22", value="2200", $$hashKey="018"}
];
var myObj = _.object(_.pluck(myArray, 'key'), _.pluck(myArray, 'value'));
answered May 24 '16 at 3:52
PaulPaul
14.7k106086
14.7k106086
add a comment |
add a comment |
Here you go:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
1
Because that's how you solve this problem. About the naming for example:arrayis a reserved keyword so people usearrinstead. etc.
– pstadler
Nov 9 '13 at 10:36
add a comment |
Here you go:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
1
Because that's how you solve this problem. About the naming for example:arrayis a reserved keyword so people usearrinstead. etc.
– pstadler
Nov 9 '13 at 10:36
add a comment |
Here you go:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
Here you go:
var arr = [{ key: "11", value: "1100" }, { key: "22", value: "2200" }];
var result = {};
for (var i=0, len=arr.length; i < len; i++) {
result[arr[i].key] = arr[i].value;
}
console.log(result); // {11: "1000", 22: "2200"}
answered Nov 9 '13 at 10:00
pstadlerpstadler
551418
551418
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
1
Because that's how you solve this problem. About the naming for example:arrayis a reserved keyword so people usearrinstead. etc.
– pstadler
Nov 9 '13 at 10:36
add a comment |
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
1
Because that's how you solve this problem. About the naming for example:arrayis a reserved keyword so people usearrinstead. etc.
– pstadler
Nov 9 '13 at 10:36
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
This surprisingly resembles the answer someone posted like 5 minutes ago
– Alexander
Nov 9 '13 at 10:04
1
1
Because that's how you solve this problem. About the naming for example:
array is a reserved keyword so people use arr instead. etc.– pstadler
Nov 9 '13 at 10:36
Because that's how you solve this problem. About the naming for example:
array is a reserved keyword so people use arr instead. etc.– pstadler
Nov 9 '13 at 10:36
add a comment |
Here's how to dynamically accept the above as a string and interpolate it into an object:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
http://jsfiddle.net/3QKmX/1/
add a comment |
Here's how to dynamically accept the above as a string and interpolate it into an object:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
http://jsfiddle.net/3QKmX/1/
add a comment |
Here's how to dynamically accept the above as a string and interpolate it into an object:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
http://jsfiddle.net/3QKmX/1/
Here's how to dynamically accept the above as a string and interpolate it into an object:
var stringObject = '[Object { key="11", value="1100", $$hashKey="00X"}, Object { key="22", value="2200", $$hashKey="018"}]';
function interpolateStringObject(stringObject) {
var jsObj = {};
var processedObj = stringObject.split("[Object { ");
processedObj = processedObj[1].split("},");
$.each(processedObj, function (i, v) {
jsObj[v.split("key=")[1].split(",")[0]] = v.split("value=")[1].split(",")[0].replace(/"/g,'');
});
return jsObj
}
var t = interpolateStringObject(stringObject); //t is the object you want
http://jsfiddle.net/3QKmX/1/
answered Nov 9 '13 at 10:14
silicakessilicakes
1,90611527
1,90611527
add a comment |
add a comment |
// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
add a comment |
// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
add a comment |
// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)// original
var arr = [{
key: '11',
value: '1100',
$$hashKey: '00X'
},
{
key: '22',
value: '2200',
$$hashKey: '018'
}
];
// My solution
var obj = {};
for (let i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
console.log(obj)edited Jul 4 '18 at 18:24
Roshana Pitigala
4,69462448
4,69462448
answered Jul 4 '18 at 13:39
omermindivanliomermindivanli
12
12
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
add a comment |
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
– Roshana Pitigala
Jul 4 '18 at 18:23
add a comment |
var obj = [
{id: 1 , name : 'gaju' , class : 'kv'},
{id: 2 , name : 'tushar' , class : 'Super'}
];
function conv(obj,arg){
var temp= {};
var output= {}
for(key in obj){
temp[key] = obj[key];
};
var i=0;
for(key in temp){
var aa = {};
var value = temp[key][arg];
aa[value] = temp[i];
//console.log(aa[value][arg] );
output[aa[value][arg]]= aa[value];
i++;
}
console.log(output);
}
calling function
conv(obj,'class');
add a comment |
var obj = [
{id: 1 , name : 'gaju' , class : 'kv'},
{id: 2 , name : 'tushar' , class : 'Super'}
];
function conv(obj,arg){
var temp= {};
var output= {}
for(key in obj){
temp[key] = obj[key];
};
var i=0;
for(key in temp){
var aa = {};
var value = temp[key][arg];
aa[value] = temp[i];
//console.log(aa[value][arg] );
output[aa[value][arg]]= aa[value];
i++;
}
console.log(output);
}
calling function
conv(obj,'class');
add a comment |
var obj = [
{id: 1 , name : 'gaju' , class : 'kv'},
{id: 2 , name : 'tushar' , class : 'Super'}
];
function conv(obj,arg){
var temp= {};
var output= {}
for(key in obj){
temp[key] = obj[key];
};
var i=0;
for(key in temp){
var aa = {};
var value = temp[key][arg];
aa[value] = temp[i];
//console.log(aa[value][arg] );
output[aa[value][arg]]= aa[value];
i++;
}
console.log(output);
}
calling function
conv(obj,'class');
var obj = [
{id: 1 , name : 'gaju' , class : 'kv'},
{id: 2 , name : 'tushar' , class : 'Super'}
];
function conv(obj,arg){
var temp= {};
var output= {}
for(key in obj){
temp[key] = obj[key];
};
var i=0;
for(key in temp){
var aa = {};
var value = temp[key][arg];
aa[value] = temp[i];
//console.log(aa[value][arg] );
output[aa[value][arg]]= aa[value];
i++;
}
console.log(output);
}
calling function
conv(obj,'class');
answered Nov 13 '18 at 10:32
Gajender SinghGajender Singh
32525
32525
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19874555%2fhow-do-i-convert-array-of-objects-into-one-object-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what language you wanna use in order to convert it?
– silicakes
Nov 9 '13 at 9:47
@Mike86 I want to use JavaScript
– Vijay Chouhan
Nov 9 '13 at 9:48
Do you accept the above as a string?
– silicakes
Nov 9 '13 at 9:53
By the way, those are Objects
– Alexander
Nov 9 '13 at 9:55
2
[{key:"11", value:"1100"}, {key:"22", value:"2200"}].reduce(function(m,v){m[v.key] = v.value; return m;}, {})– david
Nov 9 '13 at 9:57