How can I convert an Array of Key Value pairs to actual Arrayof object with values of first array as keys?
up vote
-1
down vote
favorite
My backend is returning the data as below.
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
How can I convert this to an Objectof Object as below
{BOF:true,CM:true, ...}
Thanks in advance.
javascript arrays object
add a comment |
up vote
-1
down vote
favorite
My backend is returning the data as below.
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
How can I convert this to an Objectof Object as below
{BOF:true,CM:true, ...}
Thanks in advance.
javascript arrays object
1
why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48
In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50
Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52
please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58
or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
My backend is returning the data as below.
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
How can I convert this to an Objectof Object as below
{BOF:true,CM:true, ...}
Thanks in advance.
javascript arrays object
My backend is returning the data as below.
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
How can I convert this to an Objectof Object as below
{BOF:true,CM:true, ...}
Thanks in advance.
javascript arrays object
javascript arrays object
edited Nov 19 at 11:51
asked Nov 19 at 11:47
Mahesh B
4019
4019
1
why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48
In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50
Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52
please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58
or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58
add a comment |
1
why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48
In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50
Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52
please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58
or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58
1
1
why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48
why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48
In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50
In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50
Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52
Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52
please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58
please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58
or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58
or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
You could use some ES6 syntax, using Object.assign
, spread syntax, computed properties, and then it really gets very functional programming style:
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
Why would you useObject.assign
?
– trichetriche
Nov 19 at 11:52
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
my bad, didn't see his edit to ask for an object and not an array of object. Although I still preferreduce
...
– trichetriche
Nov 19 at 11:55
1
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
|
show 1 more comment
up vote
1
down vote
Altough you can find the answer in the documentation of Array
(disclaimer: it's map
), the syntax is a bit tricky for a one-liner.
Here you go :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
As for your edit, if you want to create an object, use reduce
instead :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
add a comment |
up vote
0
down vote
Please always show your attempt in OP so that we get to know which direction you were moving..
You can use "Array.map" and "Object.assign" like below
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
You could use some ES6 syntax, using Object.assign
, spread syntax, computed properties, and then it really gets very functional programming style:
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
Why would you useObject.assign
?
– trichetriche
Nov 19 at 11:52
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
my bad, didn't see his edit to ask for an object and not an array of object. Although I still preferreduce
...
– trichetriche
Nov 19 at 11:55
1
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
|
show 1 more comment
up vote
4
down vote
You could use some ES6 syntax, using Object.assign
, spread syntax, computed properties, and then it really gets very functional programming style:
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
Why would you useObject.assign
?
– trichetriche
Nov 19 at 11:52
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
my bad, didn't see his edit to ask for an object and not an array of object. Although I still preferreduce
...
– trichetriche
Nov 19 at 11:55
1
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
|
show 1 more comment
up vote
4
down vote
up vote
4
down vote
You could use some ES6 syntax, using Object.assign
, spread syntax, computed properties, and then it really gets very functional programming style:
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
You could use some ES6 syntax, using Object.assign
, spread syntax, computed properties, and then it really gets very functional programming style:
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
edited Nov 19 at 13:20
answered Nov 19 at 11:49
trincot
113k1477109
113k1477109
Why would you useObject.assign
?
– trichetriche
Nov 19 at 11:52
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
my bad, didn't see his edit to ask for an object and not an array of object. Although I still preferreduce
...
– trichetriche
Nov 19 at 11:55
1
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
|
show 1 more comment
Why would you useObject.assign
?
– trichetriche
Nov 19 at 11:52
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
my bad, didn't see his edit to ask for an object and not an array of object. Although I still preferreduce
...
– trichetriche
Nov 19 at 11:55
1
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
Why would you use
Object.assign
?– trichetriche
Nov 19 at 11:52
Why would you use
Object.assign
?– trichetriche
Nov 19 at 11:52
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
It just is easy for converting an array of objects to one merged object.
– trincot
Nov 19 at 11:53
my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer
reduce
...– trichetriche
Nov 19 at 11:55
my bad, didn't see his edit to ask for an object and not an array of object. Although I still prefer
reduce
...– trichetriche
Nov 19 at 11:55
1
1
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
Sure, there are many ways to do it, and preferences differ.
– trincot
Nov 19 at 11:56
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
totally, +1 for that :)
– trichetriche
Nov 19 at 11:57
|
show 1 more comment
up vote
1
down vote
Altough you can find the answer in the documentation of Array
(disclaimer: it's map
), the syntax is a bit tricky for a one-liner.
Here you go :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
As for your edit, if you want to create an object, use reduce
instead :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
add a comment |
up vote
1
down vote
Altough you can find the answer in the documentation of Array
(disclaimer: it's map
), the syntax is a bit tricky for a one-liner.
Here you go :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
As for your edit, if you want to create an object, use reduce
instead :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
add a comment |
up vote
1
down vote
up vote
1
down vote
Altough you can find the answer in the documentation of Array
(disclaimer: it's map
), the syntax is a bit tricky for a one-liner.
Here you go :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
As for your edit, if you want to create an object, use reduce
instead :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
Altough you can find the answer in the documentation of Array
(disclaimer: it's map
), the syntax is a bit tricky for a one-liner.
Here you go :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
As for your edit, if you want to create an object, use reduce
instead :
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.reduce((acc, curr) => ({ ...acc, [curr.PermCode]: curr.IsvalidPerm }), {});
console.log(formatted);
answered Nov 19 at 11:50
trichetriche
23.7k41949
23.7k41949
add a comment |
add a comment |
up vote
0
down vote
Please always show your attempt in OP so that we get to know which direction you were moving..
You can use "Array.map" and "Object.assign" like below
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
add a comment |
up vote
0
down vote
Please always show your attempt in OP so that we get to know which direction you were moving..
You can use "Array.map" and "Object.assign" like below
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
add a comment |
up vote
0
down vote
up vote
0
down vote
Please always show your attempt in OP so that we get to know which direction you were moving..
You can use "Array.map" and "Object.assign" like below
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
Please always show your attempt in OP so that we get to know which direction you were moving..
You can use "Array.map" and "Object.assign" like below
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)
answered Nov 19 at 11:51
Nitish Narang
2,061611
2,061611
add a comment |
add a comment |
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%2f53373981%2fhow-can-i-convert-an-array-of-key-value-pairs-to-actual-arrayof-object-with-valu%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
1
why the array in the result? what have you tried?
– Nina Scholz
Nov 19 at 11:48
In angular I need to enable and disable things based on the user permissions. But my backend is returning the data with the column names. I need it as an array of Objects with true/false status.
– Mahesh B
Nov 19 at 11:50
Yes I realized now. I need Object not the array. Corrected my question.
– Mahesh B
Nov 19 at 11:52
please close as dupe for stackoverflow.com/questions/47133075/…
– Nina Scholz
Nov 19 at 11:58
or stackoverflow.com/questions/47365354/…
– Nina Scholz
Nov 19 at 11:58