convert map into list of objectsJavaScript/lodash data transformationDetecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How can I convert a string to boolean in JavaScript?How do I correctly clone a JavaScript object?Checking if a key exists in a JavaScript object?Convert form data to JavaScript object with jQueryStoring Objects in HTML5 localStorageHow to check if an object is an array?
[Future]Historical experience as a guide to warship design?
What do three diagonal dots above a letter mean in the "Misal rico de Cisneros" (Spain, 1518)?
Misrepresented my work history
LMOP: what beasts live in Neverwinter Wood?
Should I include code in my research paper?
Number of short hairs coming out of the base of the head Tefillin
Is this a reference to the film Alien in the novel 2010 Odyssey Two?
What is the minimum time required for final wash in film development?
Backspace functionality in normal mode
Would a carnivorous diet be able to support a giant worm?
Distinguish the explanations of Galadriel's test in LotR
Why did people still chant "Lock her up" at Trump rallies in 2019?
How to know if blackberries are safe to eat
Why did Old English lose both thorn and eth?
Why does wrapping Aluminium foil around my food help it keep warm, aluminium be good conductor should have no effect?
Using Open with a filename that contains :
Why archangel Michael didn't save Jesus when he was crucified?
Why is the ladder of the LM always in the dark side of the LM?
Why do we need common sense in AI?
Through: how to use it with subtraction of functions?
Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?
Why weren't bootable game disks ever a thing on the IBM PC?
What is the parallel of Day of the Dead with Stranger things?
One night in Baker Street
convert map into list of objects
JavaScript/lodash data transformationDetecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How can I convert a string to boolean in JavaScript?How do I correctly clone a JavaScript object?Checking if a key exists in a JavaScript object?Convert form data to JavaScript object with jQueryStoring Objects in HTML5 localStorageHow to check if an object is an array?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Input:
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
Output
[
"a": 1,
"b": 2,
"type": 8
,
"a": 3,
"b": 4,
"type": 13
]
I tried to do nested map but no luck. Please help if any straightforward way to do it.
result = _.map(input, temp => _.map(temp, obj => _.assign(_.pick(obj, ['a', 'b']))));
javascript lodash
add a comment |
Input:
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
Output
[
"a": 1,
"b": 2,
"type": 8
,
"a": 3,
"b": 4,
"type": 13
]
I tried to do nested map but no luck. Please help if any straightforward way to do it.
result = _.map(input, temp => _.map(temp, obj => _.assign(_.pick(obj, ['a', 'b']))));
javascript lodash
@JaromandaX lodash map does work on objects
– charlietfl
Mar 26 at 0:55
oh, does it ... my bad
– Jaromanda X
Mar 26 at 0:56
start with Object.values(" your object") see where that goes I guess...
– Jacob
Mar 26 at 1:01
your "attempt" is close, except you didn't even "attempt" to addtype
at all ...result = _.map(input, (temp, key) => _.map(temp, obj => _.assign(type: +key, _.pick(obj, ['a', 'b']))))
– Jaromanda X
Mar 26 at 1:02
add a comment |
Input:
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
Output
[
"a": 1,
"b": 2,
"type": 8
,
"a": 3,
"b": 4,
"type": 13
]
I tried to do nested map but no luck. Please help if any straightforward way to do it.
result = _.map(input, temp => _.map(temp, obj => _.assign(_.pick(obj, ['a', 'b']))));
javascript lodash
Input:
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
Output
[
"a": 1,
"b": 2,
"type": 8
,
"a": 3,
"b": 4,
"type": 13
]
I tried to do nested map but no luck. Please help if any straightforward way to do it.
result = _.map(input, temp => _.map(temp, obj => _.assign(_.pick(obj, ['a', 'b']))));
javascript lodash
javascript lodash
edited Mar 26 at 0:45
Jwalin Shah
asked Mar 26 at 0:41
Jwalin ShahJwalin Shah
1,53812 silver badges19 bronze badges
1,53812 silver badges19 bronze badges
@JaromandaX lodash map does work on objects
– charlietfl
Mar 26 at 0:55
oh, does it ... my bad
– Jaromanda X
Mar 26 at 0:56
start with Object.values(" your object") see where that goes I guess...
– Jacob
Mar 26 at 1:01
your "attempt" is close, except you didn't even "attempt" to addtype
at all ...result = _.map(input, (temp, key) => _.map(temp, obj => _.assign(type: +key, _.pick(obj, ['a', 'b']))))
– Jaromanda X
Mar 26 at 1:02
add a comment |
@JaromandaX lodash map does work on objects
– charlietfl
Mar 26 at 0:55
oh, does it ... my bad
– Jaromanda X
Mar 26 at 0:56
start with Object.values(" your object") see where that goes I guess...
– Jacob
Mar 26 at 1:01
your "attempt" is close, except you didn't even "attempt" to addtype
at all ...result = _.map(input, (temp, key) => _.map(temp, obj => _.assign(type: +key, _.pick(obj, ['a', 'b']))))
– Jaromanda X
Mar 26 at 1:02
@JaromandaX lodash map does work on objects
– charlietfl
Mar 26 at 0:55
@JaromandaX lodash map does work on objects
– charlietfl
Mar 26 at 0:55
oh, does it ... my bad
– Jaromanda X
Mar 26 at 0:56
oh, does it ... my bad
– Jaromanda X
Mar 26 at 0:56
start with Object.values(" your object") see where that goes I guess...
– Jacob
Mar 26 at 1:01
start with Object.values(" your object") see where that goes I guess...
– Jacob
Mar 26 at 1:01
your "attempt" is close, except you didn't even "attempt" to add
type
at all ... result = _.map(input, (temp, key) => _.map(temp, obj => _.assign(type: +key, _.pick(obj, ['a', 'b']))))
– Jaromanda X
Mar 26 at 1:02
your "attempt" is close, except you didn't even "attempt" to add
type
at all ... result = _.map(input, (temp, key) => _.map(temp, obj => _.assign(type: +key, _.pick(obj, ['a', 'b']))))
– Jaromanda X
Mar 26 at 1:02
add a comment |
2 Answers
2
active
oldest
votes
Here's a technique that should work:
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
As pointed out in another answer, if your target environments don't support flat
, you can replace this:
.flat()
with this:
.reduce((a, b) => a.concat(b), [])
add a comment |
You can use _.flatMap()
to iterate the object, and flatten the results. Inside the flatMap iterate the values with _.map()
, and use _.assign()
to combine the original object and the type:
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
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%2f55348314%2fconvert-map-into-list-of-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a technique that should work:
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
As pointed out in another answer, if your target environments don't support flat
, you can replace this:
.flat()
with this:
.reduce((a, b) => a.concat(b), [])
add a comment |
Here's a technique that should work:
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
As pointed out in another answer, if your target environments don't support flat
, you can replace this:
.flat()
with this:
.reduce((a, b) => a.concat(b), [])
add a comment |
Here's a technique that should work:
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
As pointed out in another answer, if your target environments don't support flat
, you can replace this:
.flat()
with this:
.reduce((a, b) => a.concat(b), [])
Here's a technique that should work:
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
As pointed out in another answer, if your target environments don't support flat
, you can replace this:
.flat()
with this:
.reduce((a, b) => a.concat(b), [])
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
const transform = (input) => Object.entries(input)
.map(([key, val]) => val.map(v => (...v, type: key)))
.flat()
const input = "13": ["a": 3, "b": 4], "8": ["a": 1, "b": 2]
console.log(transform(input))
answered Mar 26 at 0:51
Scott SauyetScott Sauyet
23.7k2 gold badges27 silver badges61 bronze badges
23.7k2 gold badges27 silver badges61 bronze badges
add a comment |
add a comment |
You can use _.flatMap()
to iterate the object, and flatten the results. Inside the flatMap iterate the values with _.map()
, and use _.assign()
to combine the original object and the type:
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
You can use _.flatMap()
to iterate the object, and flatten the results. Inside the flatMap iterate the values with _.map()
, and use _.assign()
to combine the original object and the type:
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
add a comment |
You can use _.flatMap()
to iterate the object, and flatten the results. Inside the flatMap iterate the values with _.map()
, and use _.assign()
to combine the original object and the type:
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
You can use _.flatMap()
to iterate the object, and flatten the results. Inside the flatMap iterate the values with _.map()
, and use _.assign()
to combine the original object and the type:
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const input =
"8": [
"a": 1,
"b": 2
],
"13": [
"a": 3,
"b": 4
]
const result = _.flatMap(input, // iterate the object and flatten the results
(values, type) => _.map( // iterate the arrays
values, v => _.assign( type , v) // combine the type with the object
)
)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
edited Mar 26 at 15:06
answered Mar 26 at 6:03
Ori DroriOri Drori
89.4k15 gold badges99 silver badges105 bronze badges
89.4k15 gold badges99 silver badges105 bronze badges
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%2f55348314%2fconvert-map-into-list-of-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
@JaromandaX lodash map does work on objects
– charlietfl
Mar 26 at 0:55
oh, does it ... my bad
– Jaromanda X
Mar 26 at 0:56
start with Object.values(" your object") see where that goes I guess...
– Jacob
Mar 26 at 1:01
your "attempt" is close, except you didn't even "attempt" to add
type
at all ...result = _.map(input, (temp, key) => _.map(temp, obj => _.assign(type: +key, _.pick(obj, ['a', 'b']))))
– Jaromanda X
Mar 26 at 1:02