How to convert array of objects to single object which has dynamic key in typescriptDoes JavaScript Guarantee Object Property Order?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueHow do I empty an array in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?Find object by id in an array of JavaScript objects
Selecting a secure PIN for building access
Why are there synthetic chemicals in our bodies? Where do they come from?
A non-technological, repeating, phenomenon in the sky, holding its position in the sky for hours
How to get SEEK accessing converted ID via view
Can a cyclic Amine form an Amide?
What happened to Rhaegal?
Stark VS Thanos
Can PCs use nonmagical armor and weapons looted from monsters?
Why is the SNP putting so much emphasis on currency plans?
Can commander tax be proliferated?
Is thermodynamics only applicable to systems in equilibrium?
How do you center multiple equations that have multiple steps?
What happens if I start too many background jobs?
Any examples of headwear for races with animal ears?
Why do freehub and cassette have only one position that matches?
Packet sniffer for MacOS Mojave and above
Was the ancestor of SCSI, the SASI protocol, nothing more than a draft?
Survey Confirmation - Emphasize the question or the answer?
Is it cheaper to drop cargo than to land it?
Historically, were women trained for obligatory wars? Or did they serve some other military function?
Hang 20lb projector screen on Hardieplank
Unexpected email from Yorkshire Bank
Accidentally deleted the "/usr/share" folder
Unidentified items in bicycle tube repair kit
How to convert array of objects to single object which has dynamic key in typescript
Does JavaScript Guarantee Object Property Order?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueHow do I empty an array in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?Find object by id in an array of JavaScript objects
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question might be similar to frequently asked one, but this one has some different approach.
In my angular 7 application, I have the following 5 arrays which needs to be converted to the below single object with dynamic key based on the id.
"enabled-41": true,
"enabled-42": true,
"enabled-43": true,
"enabled-44": true,
"enabled-45": false,
"abc-41": "some description 1",
"abc-42": "some description 12",
"abc-43": "some description 123",
"abc-44": "some description 1234",
"abc-45": null,
"def-41": "some description 2",
"def-42": "some description 23",
"def-43": "some description 234",
"def-44": "some description 2345",
"def-45": null,
"type-41": "def",
"type-42": "abc",
"type-43": "def",
"type-44": "abc",
"type-45": null,
"weight-41": "25",
"weight-42": "25",
"weight-43": "25",
"weight-44": "25",
"weight-45": null
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[item] = item.value;
return obj;
, )
console.log(result);
I have tried using reduce function, but cannot able to get the right way to convert to a single object with the above format based on dynamic key (joining id with hypen).
Can someone help me with this?
javascript arrays typescript object ecmascript-6
add a comment |
This question might be similar to frequently asked one, but this one has some different approach.
In my angular 7 application, I have the following 5 arrays which needs to be converted to the below single object with dynamic key based on the id.
"enabled-41": true,
"enabled-42": true,
"enabled-43": true,
"enabled-44": true,
"enabled-45": false,
"abc-41": "some description 1",
"abc-42": "some description 12",
"abc-43": "some description 123",
"abc-44": "some description 1234",
"abc-45": null,
"def-41": "some description 2",
"def-42": "some description 23",
"def-43": "some description 234",
"def-44": "some description 2345",
"def-45": null,
"type-41": "def",
"type-42": "abc",
"type-43": "def",
"type-44": "abc",
"type-45": null,
"weight-41": "25",
"weight-42": "25",
"weight-43": "25",
"weight-44": "25",
"weight-45": null
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[item] = item.value;
return obj;
, )
console.log(result);
I have tried using reduce function, but cannot able to get the right way to convert to a single object with the above format based on dynamic key (joining id with hypen).
Can someone help me with this?
javascript arrays typescript object ecmascript-6
What about theCriteria
object? What should happen? Should it be discarded?
– Jack Bashford
1 hour ago
Yes Criteria object can be discarded and want to get the single object as above
– UI_Dev
1 hour ago
add a comment |
This question might be similar to frequently asked one, but this one has some different approach.
In my angular 7 application, I have the following 5 arrays which needs to be converted to the below single object with dynamic key based on the id.
"enabled-41": true,
"enabled-42": true,
"enabled-43": true,
"enabled-44": true,
"enabled-45": false,
"abc-41": "some description 1",
"abc-42": "some description 12",
"abc-43": "some description 123",
"abc-44": "some description 1234",
"abc-45": null,
"def-41": "some description 2",
"def-42": "some description 23",
"def-43": "some description 234",
"def-44": "some description 2345",
"def-45": null,
"type-41": "def",
"type-42": "abc",
"type-43": "def",
"type-44": "abc",
"type-45": null,
"weight-41": "25",
"weight-42": "25",
"weight-43": "25",
"weight-44": "25",
"weight-45": null
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[item] = item.value;
return obj;
, )
console.log(result);
I have tried using reduce function, but cannot able to get the right way to convert to a single object with the above format based on dynamic key (joining id with hypen).
Can someone help me with this?
javascript arrays typescript object ecmascript-6
This question might be similar to frequently asked one, but this one has some different approach.
In my angular 7 application, I have the following 5 arrays which needs to be converted to the below single object with dynamic key based on the id.
"enabled-41": true,
"enabled-42": true,
"enabled-43": true,
"enabled-44": true,
"enabled-45": false,
"abc-41": "some description 1",
"abc-42": "some description 12",
"abc-43": "some description 123",
"abc-44": "some description 1234",
"abc-45": null,
"def-41": "some description 2",
"def-42": "some description 23",
"def-43": "some description 234",
"def-44": "some description 2345",
"def-45": null,
"type-41": "def",
"type-42": "abc",
"type-43": "def",
"type-44": "abc",
"type-45": null,
"weight-41": "25",
"weight-42": "25",
"weight-43": "25",
"weight-44": "25",
"weight-45": null
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[item] = item.value;
return obj;
, )
console.log(result);
I have tried using reduce function, but cannot able to get the right way to convert to a single object with the above format based on dynamic key (joining id with hypen).
Can someone help me with this?
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[item] = item.value;
return obj;
, )
console.log(result);
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[item] = item.value;
return obj;
, )
console.log(result);
javascript arrays typescript object ecmascript-6
javascript arrays typescript object ecmascript-6
edited 1 hour ago
Jack Bashford
19.2k52050
19.2k52050
asked 1 hour ago
UI_DevUI_Dev
1,31792453
1,31792453
What about theCriteria
object? What should happen? Should it be discarded?
– Jack Bashford
1 hour ago
Yes Criteria object can be discarded and want to get the single object as above
– UI_Dev
1 hour ago
add a comment |
What about theCriteria
object? What should happen? Should it be discarded?
– Jack Bashford
1 hour ago
Yes Criteria object can be discarded and want to get the single object as above
– UI_Dev
1 hour ago
What about the
Criteria
object? What should happen? Should it be discarded?– Jack Bashford
1 hour ago
What about the
Criteria
object? What should happen? Should it be discarded?– Jack Bashford
1 hour ago
Yes Criteria object can be discarded and want to get the single object as above
– UI_Dev
1 hour ago
Yes Criteria object can be discarded and want to get the single object as above
– UI_Dev
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
You code is almost there. But object keys order is not guaranteed. Inside the reduce callback function add the keys in the accumulator and corresponding value.
Use template literals & square notation while creating the object keys
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
Thank you... @brk
– UI_Dev
58 mins ago
why downvote? Please clarify
– brk
24 mins ago
add a comment |
You can use reduce
with Object.keys
, and place all keys you wish to exclude in an array and check against that:
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
Thanks Jack...!
– UI_Dev
58 mins ago
add a comment |
Assuming you want to exclude all the properties whose value is an object
maybe you can go with this generic idea that uses Object.entries()
to traverse the inner objects and some destructuring
features.
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
Thanks Shidersz..!
– UI_Dev
58 mins ago
add a comment |
Oh, man... I just got beat. Here's my solution.
let arr= [] // hold the final object array
let keys = [] // temp item to hold the value of each key
// iterate over each key
Object.keys(input).forEach((key) =>
let pair = key.split('-') // split the key into the real key and the index
// if the index isn't in the array, push it there (this keeps the same order)
if (keys.indexOf(pair[1])===-1)
keys.push(pair[1])
// use object.assign to add the keys to the existing object in the right place in the array.
arr[keys.indexOf(pair[1])] = Object.assign(, arr[keys.indexOf(pair[1])], [pair[0]]: input[key], id: pair[1] )
)
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%2f55914053%2fhow-to-convert-array-of-objects-to-single-object-which-has-dynamic-key-in-typesc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You code is almost there. But object keys order is not guaranteed. Inside the reduce callback function add the keys in the accumulator and corresponding value.
Use template literals & square notation while creating the object keys
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
Thank you... @brk
– UI_Dev
58 mins ago
why downvote? Please clarify
– brk
24 mins ago
add a comment |
You code is almost there. But object keys order is not guaranteed. Inside the reduce callback function add the keys in the accumulator and corresponding value.
Use template literals & square notation while creating the object keys
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
Thank you... @brk
– UI_Dev
58 mins ago
why downvote? Please clarify
– brk
24 mins ago
add a comment |
You code is almost there. But object keys order is not guaranteed. Inside the reduce callback function add the keys in the accumulator and corresponding value.
Use template literals & square notation while creating the object keys
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
You code is almost there. But object keys order is not guaranteed. Inside the reduce callback function add the keys in the accumulator and corresponding value.
Use template literals & square notation while creating the object keys
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
let arr = [
"id": 41,
"abc": "some description 1",
"def": "some description 2",
"type": "def",
"Criteria":
"id": 5,
"question": "follow-up",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 42,
"abc": "some description 12",
"def": "some description 23",
"type": "abc",
"Criteria":
"id": 1,
"question": "coverage",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 43,
"abc": "some description 123",
"def": "some description 234",
"type": "def",
"Criteria":
"id": 4,
"question": "Price",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 44,
"abc": "some description 1234",
"def": "some description 2345",
"type": "abc",
"Criteria":
"id": 3,
"question": "Exchange",
"definition": "definition content",
"status": true
,
"weight": 25,
"enabled": true
,
"id": 45,
"Criteria":
"id": 2,
"definition": "definition conent",
"question": "Random",
"status": true
,
"type": null,
"abc": null,
"def": null,
"weight": 0,
"enabled": false
];
let result = arr.reduce(function(obj, item)
obj[`enabled-$item.id`] = item.enabled;
obj[`abc-$item.id`] = item.abc;
obj[`def-$item.id`] = item.def;
obj[`type-$item.id`] = item.type;
obj[`weight-$item.id`] = item.weight;
return obj;
, );
console.log(result)
answered 1 hour ago
brkbrk
30.8k32446
30.8k32446
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
Thank you... @brk
– UI_Dev
58 mins ago
why downvote? Please clarify
– brk
24 mins ago
add a comment |
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
Thank you... @brk
– UI_Dev
58 mins ago
why downvote? Please clarify
– brk
24 mins ago
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
Thanks, is there any possibility with the object key order?
– UI_Dev
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
@UI_Dev hopefully this link stackoverflow.com/questions/5525795/… will help you
– brk
1 hour ago
Thank you... @brk
– UI_Dev
58 mins ago
Thank you... @brk
– UI_Dev
58 mins ago
why downvote? Please clarify
– brk
24 mins ago
why downvote? Please clarify
– brk
24 mins ago
add a comment |
You can use reduce
with Object.keys
, and place all keys you wish to exclude in an array and check against that:
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
Thanks Jack...!
– UI_Dev
58 mins ago
add a comment |
You can use reduce
with Object.keys
, and place all keys you wish to exclude in an array and check against that:
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
Thanks Jack...!
– UI_Dev
58 mins ago
add a comment |
You can use reduce
with Object.keys
, and place all keys you wish to exclude in an array and check against that:
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
You can use reduce
with Object.keys
, and place all keys you wish to exclude in an array and check against that:
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
let arr = ["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let exclude = ["id", "Criteria"];
let result = arr.reduce((acc, curr) =>
let id = curr.id;
Object.entries(curr).forEach(([k, v]) =>
if (!exclude.includes(k)) acc[`$k-$id`] = v;
);
return acc;
, );
console.log(result);
answered 1 hour ago
Jack BashfordJack Bashford
19.2k52050
19.2k52050
Thanks Jack...!
– UI_Dev
58 mins ago
add a comment |
Thanks Jack...!
– UI_Dev
58 mins ago
Thanks Jack...!
– UI_Dev
58 mins ago
Thanks Jack...!
– UI_Dev
58 mins ago
add a comment |
Assuming you want to exclude all the properties whose value is an object
maybe you can go with this generic idea that uses Object.entries()
to traverse the inner objects and some destructuring
features.
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
Thanks Shidersz..!
– UI_Dev
58 mins ago
add a comment |
Assuming you want to exclude all the properties whose value is an object
maybe you can go with this generic idea that uses Object.entries()
to traverse the inner objects and some destructuring
features.
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
Thanks Shidersz..!
– UI_Dev
58 mins ago
add a comment |
Assuming you want to exclude all the properties whose value is an object
maybe you can go with this generic idea that uses Object.entries()
to traverse the inner objects and some destructuring
features.
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
Assuming you want to exclude all the properties whose value is an object
maybe you can go with this generic idea that uses Object.entries()
to traverse the inner objects and some destructuring
features.
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
let arr=["id":41,"abc":"some description 1","def":"some description 2","type":"def","Criteria":"id":5,"question":"follow-up","definition":"definition content","status":true,"weight":25,"enabled":true,"id":42,"abc":"some description 12","def":"some description 23","type":"abc","Criteria":"id":1,"question":"coverage","definition":"definition content","status":true,"weight":25,"enabled":true,"id":43,"abc":"some description 123","def":"some description 234","type":"def","Criteria":"id":4,"question":"Price","definition":"definition content","status":true,"weight":25,"enabled":true,"id":44,"abc":"some description 1234","def":"some description 2345","type":"abc","Criteria":"id":3,"question":"Exchange","definition":"definition content","status":true,"weight":25,"enabled":true,"id":45,"Criteria":"id":2,"definition":"definition conent","question":"Random","status":true,"type":null,"abc":null,"def":null,"weight":0,"enabled":false];
let result = arr.reduce((obj, id, ...rest) =>
Object.entries(rest).forEach(([k, v]) =>
if (Object(v) !== v) obj[`$k-$id`] = v;
);
return obj;
, );
console.log(result);
.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;
answered 1 hour ago
ShiderszShidersz
11.2k2933
11.2k2933
Thanks Shidersz..!
– UI_Dev
58 mins ago
add a comment |
Thanks Shidersz..!
– UI_Dev
58 mins ago
Thanks Shidersz..!
– UI_Dev
58 mins ago
Thanks Shidersz..!
– UI_Dev
58 mins ago
add a comment |
Oh, man... I just got beat. Here's my solution.
let arr= [] // hold the final object array
let keys = [] // temp item to hold the value of each key
// iterate over each key
Object.keys(input).forEach((key) =>
let pair = key.split('-') // split the key into the real key and the index
// if the index isn't in the array, push it there (this keeps the same order)
if (keys.indexOf(pair[1])===-1)
keys.push(pair[1])
// use object.assign to add the keys to the existing object in the right place in the array.
arr[keys.indexOf(pair[1])] = Object.assign(, arr[keys.indexOf(pair[1])], [pair[0]]: input[key], id: pair[1] )
)
add a comment |
Oh, man... I just got beat. Here's my solution.
let arr= [] // hold the final object array
let keys = [] // temp item to hold the value of each key
// iterate over each key
Object.keys(input).forEach((key) =>
let pair = key.split('-') // split the key into the real key and the index
// if the index isn't in the array, push it there (this keeps the same order)
if (keys.indexOf(pair[1])===-1)
keys.push(pair[1])
// use object.assign to add the keys to the existing object in the right place in the array.
arr[keys.indexOf(pair[1])] = Object.assign(, arr[keys.indexOf(pair[1])], [pair[0]]: input[key], id: pair[1] )
)
add a comment |
Oh, man... I just got beat. Here's my solution.
let arr= [] // hold the final object array
let keys = [] // temp item to hold the value of each key
// iterate over each key
Object.keys(input).forEach((key) =>
let pair = key.split('-') // split the key into the real key and the index
// if the index isn't in the array, push it there (this keeps the same order)
if (keys.indexOf(pair[1])===-1)
keys.push(pair[1])
// use object.assign to add the keys to the existing object in the right place in the array.
arr[keys.indexOf(pair[1])] = Object.assign(, arr[keys.indexOf(pair[1])], [pair[0]]: input[key], id: pair[1] )
)
Oh, man... I just got beat. Here's my solution.
let arr= [] // hold the final object array
let keys = [] // temp item to hold the value of each key
// iterate over each key
Object.keys(input).forEach((key) =>
let pair = key.split('-') // split the key into the real key and the index
// if the index isn't in the array, push it there (this keeps the same order)
if (keys.indexOf(pair[1])===-1)
keys.push(pair[1])
// use object.assign to add the keys to the existing object in the right place in the array.
arr[keys.indexOf(pair[1])] = Object.assign(, arr[keys.indexOf(pair[1])], [pair[0]]: input[key], id: pair[1] )
)
answered 1 hour ago
tagyoureittagyoureit
1065
1065
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%2f55914053%2fhow-to-convert-array-of-objects-to-single-object-which-has-dynamic-key-in-typesc%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 about the
Criteria
object? What should happen? Should it be discarded?– Jack Bashford
1 hour ago
Yes Criteria object can be discarded and want to get the single object as above
– UI_Dev
1 hour ago