how to convert 2d array to objects
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
i have this kind of array objects:
[
{
"abc": [
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"bcd": [
{
"as df": [
"fd/f",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"fd": [
{
"fdf ff": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
}
]
to put it simply, that is an array of objects, where the objects will be inside of the array and become the index of the array. but, i want the array gone, so it will become pure object lists. so i want to convert it into something like this
{
"abc":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"bcd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"fd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
}
I have tried using flatmap but it does not work. can someone help me how to do this. answer is appreciated. many thanks
javascript arrays javascript-objects
add a comment |
i have this kind of array objects:
[
{
"abc": [
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"bcd": [
{
"as df": [
"fd/f",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"fd": [
{
"fdf ff": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
}
]
to put it simply, that is an array of objects, where the objects will be inside of the array and become the index of the array. but, i want the array gone, so it will become pure object lists. so i want to convert it into something like this
{
"abc":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"bcd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"fd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
}
I have tried using flatmap but it does not work. can someone help me how to do this. answer is appreciated. many thanks
javascript arrays javascript-objects
1
it looks like you're trying to extract the first value of an array?
– Dacre Denny
Nov 27 '18 at 1:55
something like that, i actually have lists of objects inside of the array, i want to convert the array to only have lists of object instead of putting it inside of the array
– Amam Mustofa
Nov 27 '18 at 2:02
1
You should improve the quality of your question. "Something like that" is pretty useless to us if you need to solve an explicit problem. Also, that data structure has a problem with the closing brackets and gives an error.
– Andy
Nov 27 '18 at 2:05
i have edited just know
– Amam Mustofa
Nov 27 '18 at 2:23
add a comment |
i have this kind of array objects:
[
{
"abc": [
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"bcd": [
{
"as df": [
"fd/f",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"fd": [
{
"fdf ff": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
}
]
to put it simply, that is an array of objects, where the objects will be inside of the array and become the index of the array. but, i want the array gone, so it will become pure object lists. so i want to convert it into something like this
{
"abc":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"bcd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"fd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
}
I have tried using flatmap but it does not work. can someone help me how to do this. answer is appreciated. many thanks
javascript arrays javascript-objects
i have this kind of array objects:
[
{
"abc": [
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"bcd": [
{
"as df": [
"fd/f",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
},
"fd": [
{
"fdf ff": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
]
}
]
to put it simply, that is an array of objects, where the objects will be inside of the array and become the index of the array. but, i want the array gone, so it will become pure object lists. so i want to convert it into something like this
{
"abc":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"bcd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
},
"fd":
{
"abc def": [
"sdf/sdf",
"qwe/er",
"kjf/er",
"0/1",
"df/ff",
"kkf/iei",
]
}
}
I have tried using flatmap but it does not work. can someone help me how to do this. answer is appreciated. many thanks
javascript arrays javascript-objects
javascript arrays javascript-objects
edited Nov 27 '18 at 2:28
Amam Mustofa
asked Nov 27 '18 at 1:53
Amam MustofaAmam Mustofa
347
347
1
it looks like you're trying to extract the first value of an array?
– Dacre Denny
Nov 27 '18 at 1:55
something like that, i actually have lists of objects inside of the array, i want to convert the array to only have lists of object instead of putting it inside of the array
– Amam Mustofa
Nov 27 '18 at 2:02
1
You should improve the quality of your question. "Something like that" is pretty useless to us if you need to solve an explicit problem. Also, that data structure has a problem with the closing brackets and gives an error.
– Andy
Nov 27 '18 at 2:05
i have edited just know
– Amam Mustofa
Nov 27 '18 at 2:23
add a comment |
1
it looks like you're trying to extract the first value of an array?
– Dacre Denny
Nov 27 '18 at 1:55
something like that, i actually have lists of objects inside of the array, i want to convert the array to only have lists of object instead of putting it inside of the array
– Amam Mustofa
Nov 27 '18 at 2:02
1
You should improve the quality of your question. "Something like that" is pretty useless to us if you need to solve an explicit problem. Also, that data structure has a problem with the closing brackets and gives an error.
– Andy
Nov 27 '18 at 2:05
i have edited just know
– Amam Mustofa
Nov 27 '18 at 2:23
1
1
it looks like you're trying to extract the first value of an array?
– Dacre Denny
Nov 27 '18 at 1:55
it looks like you're trying to extract the first value of an array?
– Dacre Denny
Nov 27 '18 at 1:55
something like that, i actually have lists of objects inside of the array, i want to convert the array to only have lists of object instead of putting it inside of the array
– Amam Mustofa
Nov 27 '18 at 2:02
something like that, i actually have lists of objects inside of the array, i want to convert the array to only have lists of object instead of putting it inside of the array
– Amam Mustofa
Nov 27 '18 at 2:02
1
1
You should improve the quality of your question. "Something like that" is pretty useless to us if you need to solve an explicit problem. Also, that data structure has a problem with the closing brackets and gives an error.
– Andy
Nov 27 '18 at 2:05
You should improve the quality of your question. "Something like that" is pretty useless to us if you need to solve an explicit problem. Also, that data structure has a problem with the closing brackets and gives an error.
– Andy
Nov 27 '18 at 2:05
i have edited just know
– Amam Mustofa
Nov 27 '18 at 2:23
i have edited just know
– Amam Mustofa
Nov 27 '18 at 2:23
add a comment |
1 Answer
1
active
oldest
votes
This could work:
const a = [{"abc": [{"abc def": ["sdf/sdf", "qwe/er", "kjf/er", "0/1", "df/ff", "kkf/iei"] } ] }];
a.reduce((obj, item) => { const key = Object.keys(item)[0]; const first_value = item[key][0]; return {[key]: first_value}}, {})
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
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%2f53491655%2fhow-to-convert-2d-array-to-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This could work:
const a = [{"abc": [{"abc def": ["sdf/sdf", "qwe/er", "kjf/er", "0/1", "df/ff", "kkf/iei"] } ] }];
a.reduce((obj, item) => { const key = Object.keys(item)[0]; const first_value = item[key][0]; return {[key]: first_value}}, {})
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
add a comment |
This could work:
const a = [{"abc": [{"abc def": ["sdf/sdf", "qwe/er", "kjf/er", "0/1", "df/ff", "kkf/iei"] } ] }];
a.reduce((obj, item) => { const key = Object.keys(item)[0]; const first_value = item[key][0]; return {[key]: first_value}}, {})
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
add a comment |
This could work:
const a = [{"abc": [{"abc def": ["sdf/sdf", "qwe/er", "kjf/er", "0/1", "df/ff", "kkf/iei"] } ] }];
a.reduce((obj, item) => { const key = Object.keys(item)[0]; const first_value = item[key][0]; return {[key]: first_value}}, {})
This could work:
const a = [{"abc": [{"abc def": ["sdf/sdf", "qwe/er", "kjf/er", "0/1", "df/ff", "kkf/iei"] } ] }];
a.reduce((obj, item) => { const key = Object.keys(item)[0]; const first_value = item[key][0]; return {[key]: first_value}}, {})
answered Nov 27 '18 at 2:32
DinlDinl
135
135
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
add a comment |
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
thank you for answering. but, this only works for the first value, how about the other objects
– Amam Mustofa
Nov 27 '18 at 2:50
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%2f53491655%2fhow-to-convert-2d-array-to-objects%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
it looks like you're trying to extract the first value of an array?
– Dacre Denny
Nov 27 '18 at 1:55
something like that, i actually have lists of objects inside of the array, i want to convert the array to only have lists of object instead of putting it inside of the array
– Amam Mustofa
Nov 27 '18 at 2:02
1
You should improve the quality of your question. "Something like that" is pretty useless to us if you need to solve an explicit problem. Also, that data structure has a problem with the closing brackets and gives an error.
– Andy
Nov 27 '18 at 2:05
i have edited just know
– Amam Mustofa
Nov 27 '18 at 2:23