Javascript data transformation The Next CEO of Stack OverflowHow do JavaScript closures work?How can I convert a string to boolean in JavaScript?How do I loop through or enumerate a JavaScript object?How to loop through a plain JavaScript object with the objects as members?How do I include a JavaScript file in another JavaScript file?Checking if a key exists in a JavaScript object?Convert form data to JavaScript object with jQueryWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
What happened in Rome, when the western empire "fell"?
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Complex fractions
Indicator light circuit
What does "Its cash flow is deeply negative" mean?
WOW air has ceased operation, can I get my tickets refunded?
MessageLevel in QGIS3
What benefits would be gained by using human laborers instead of drones in deep sea mining?
How does the mv command work with external drives?
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?
Multiple labels for a single equation
What connection does MS Office have to Netscape Navigator?
Bold, vivid family
Is micro rebar a better way to reinforce concrete than rebar?
Why am I allowed to create multiple unique pointers from a single object?
Which tube will fit a -(700 x 25c) wheel?
Would a completely good Muggle be able to use a wand?
Can I equip Skullclamp on a creature I am sacrificing?
Anatomically Correct Strange Women In Ponds Distributing Swords
Is it my responsibility to learn a new technology in my own time my employer wants to implement?
What is ( CFMCC ) on ILS approach chart?
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?
Unreliable Magic - Is it worth it?
Javascript data transformation
The Next CEO of Stack OverflowHow do JavaScript closures work?How can I convert a string to boolean in JavaScript?How do I loop through or enumerate a JavaScript object?How to loop through a plain JavaScript object with the objects as members?How do I include a JavaScript file in another JavaScript file?Checking if a key exists in a JavaScript object?Convert form data to JavaScript object with jQueryWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Input:
"8": [
"a": true,
"b":
"xyz": 1
,
"a": false,
"b":
"xyz": 2
],
"13": [
"b":
"xyz": 4
]
Output:
"8": [
"b":
"xyz": 2
]
How can remove first element of each key and return the few keys of the same object using javascript and lodash library?
javascript lodash
add a comment |
Input:
"8": [
"a": true,
"b":
"xyz": 1
,
"a": false,
"b":
"xyz": 2
],
"13": [
"b":
"xyz": 4
]
Output:
"8": [
"b":
"xyz": 2
]
How can remove first element of each key and return the few keys of the same object using javascript and lodash library?
javascript lodash
1
you can remove the first element of an array with array.shift
– Chris Li
Mar 21 at 17:11
Thanks for your quick response but how about above input and output. I need to remove first element of each key and return same object. If only 1 element then need to remove that element itself.
– Jwalin Shah
Mar 21 at 17:16
Anybody help me on this?
– Jwalin Shah
Mar 21 at 19:35
add a comment |
Input:
"8": [
"a": true,
"b":
"xyz": 1
,
"a": false,
"b":
"xyz": 2
],
"13": [
"b":
"xyz": 4
]
Output:
"8": [
"b":
"xyz": 2
]
How can remove first element of each key and return the few keys of the same object using javascript and lodash library?
javascript lodash
Input:
"8": [
"a": true,
"b":
"xyz": 1
,
"a": false,
"b":
"xyz": 2
],
"13": [
"b":
"xyz": 4
]
Output:
"8": [
"b":
"xyz": 2
]
How can remove first element of each key and return the few keys of the same object using javascript and lodash library?
javascript lodash
javascript lodash
edited Mar 21 at 17:29
Jwalin Shah
asked Mar 21 at 17:10
Jwalin ShahJwalin Shah
1,4541219
1,4541219
1
you can remove the first element of an array with array.shift
– Chris Li
Mar 21 at 17:11
Thanks for your quick response but how about above input and output. I need to remove first element of each key and return same object. If only 1 element then need to remove that element itself.
– Jwalin Shah
Mar 21 at 17:16
Anybody help me on this?
– Jwalin Shah
Mar 21 at 19:35
add a comment |
1
you can remove the first element of an array with array.shift
– Chris Li
Mar 21 at 17:11
Thanks for your quick response but how about above input and output. I need to remove first element of each key and return same object. If only 1 element then need to remove that element itself.
– Jwalin Shah
Mar 21 at 17:16
Anybody help me on this?
– Jwalin Shah
Mar 21 at 19:35
1
1
you can remove the first element of an array with array.shift
– Chris Li
Mar 21 at 17:11
you can remove the first element of an array with array.shift
– Chris Li
Mar 21 at 17:11
Thanks for your quick response but how about above input and output. I need to remove first element of each key and return same object. If only 1 element then need to remove that element itself.
– Jwalin Shah
Mar 21 at 17:16
Thanks for your quick response but how about above input and output. I need to remove first element of each key and return same object. If only 1 element then need to remove that element itself.
– Jwalin Shah
Mar 21 at 17:16
Anybody help me on this?
– Jwalin Shah
Mar 21 at 19:35
Anybody help me on this?
– Jwalin Shah
Mar 21 at 19:35
add a comment |
3 Answers
3
active
oldest
votes
You could use reduce
the entries returned by Object.entries()
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
If you want to mutate the original object, loop through the object using for...in
and use shift
and delete
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need keyb
. updated output
– Jwalin Shah
Mar 21 at 17:22
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
|
show 4 more comments
Without loadash do with Array#shift
and Array#foreach
- First convert obj to array using
Object.keys
- Then loop the value .And remove the first index of array using
Array#shift
- Then apply condition with array length is 0 remove the key value pair from main object
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
This is not the desired output
– mhodges
Mar 21 at 17:19
1
@mhodges now check
– prasanth
Mar 21 at 17:21
Looks good now. You could save yourself theshift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.
– mhodges
Mar 21 at 17:22
add a comment |
Use lodash's _.flow()
with _.partialRight()
to create a function that maps the values to the tail (all items but the 1st) of each array, and then uses _.omitBy()
to remove empty keys:
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.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%2f55285775%2fjavascript-data-transformation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use reduce
the entries returned by Object.entries()
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
If you want to mutate the original object, loop through the object using for...in
and use shift
and delete
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need keyb
. updated output
– Jwalin Shah
Mar 21 at 17:22
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
|
show 4 more comments
You could use reduce
the entries returned by Object.entries()
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
If you want to mutate the original object, loop through the object using for...in
and use shift
and delete
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need keyb
. updated output
– Jwalin Shah
Mar 21 at 17:22
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
|
show 4 more comments
You could use reduce
the entries returned by Object.entries()
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
If you want to mutate the original object, loop through the object using for...in
and use shift
and delete
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
You could use reduce
the entries returned by Object.entries()
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
If you want to mutate the original object, loop through the object using for...in
and use shift
and delete
like this:
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
let output = Object.entries(obj).reduce((acc, [key, value]) =>
if(value.length > 1)
acc[key] = value.slice(1)
return acc;
, )
console.log(output)
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
let obj="8":["a":!0,"b":"xyz":1,"a":!1,"b":"xyz":2],"13":["b":"xyz":4]
for (let key in obj)
obj[key].shift()
if (obj[key].length === 0)
delete obj[key]
console.log(obj)
edited Mar 21 at 17:40
answered Mar 21 at 17:17
adigaadiga
12.1k62545
12.1k62545
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need keyb
. updated output
– Jwalin Shah
Mar 21 at 17:22
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
|
show 4 more comments
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need keyb
. updated output
– Jwalin Shah
Mar 21 at 17:22
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
"How can remove first element of each key and return the same object", I believe OP wants the original object mutated
– mhodges
Mar 21 at 17:18
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need key
b
. updated output– Jwalin Shah
Mar 21 at 17:22
Yeah, seems that work and what if I need only certain keys of the object. For example, I only need key
b
. updated output– Jwalin Shah
Mar 21 at 17:22
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@mhodges, Thanks for your response, can you let me know how I can get certain keys? In your output, you are returning keys a and b both any other ways to get certain keys.
– Jwalin Shah
Mar 21 at 17:44
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, did not get you. I'm referring code with reducer.
– Jwalin Shah
Mar 21 at 17:51
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
@adiga, pls. check my question. In output, I only need object with key b not key a. I think you misunderstood my questions.
– Jwalin Shah
Mar 21 at 17:57
|
show 4 more comments
Without loadash do with Array#shift
and Array#foreach
- First convert obj to array using
Object.keys
- Then loop the value .And remove the first index of array using
Array#shift
- Then apply condition with array length is 0 remove the key value pair from main object
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
This is not the desired output
– mhodges
Mar 21 at 17:19
1
@mhodges now check
– prasanth
Mar 21 at 17:21
Looks good now. You could save yourself theshift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.
– mhodges
Mar 21 at 17:22
add a comment |
Without loadash do with Array#shift
and Array#foreach
- First convert obj to array using
Object.keys
- Then loop the value .And remove the first index of array using
Array#shift
- Then apply condition with array length is 0 remove the key value pair from main object
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
This is not the desired output
– mhodges
Mar 21 at 17:19
1
@mhodges now check
– prasanth
Mar 21 at 17:21
Looks good now. You could save yourself theshift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.
– mhodges
Mar 21 at 17:22
add a comment |
Without loadash do with Array#shift
and Array#foreach
- First convert obj to array using
Object.keys
- Then loop the value .And remove the first index of array using
Array#shift
- Then apply condition with array length is 0 remove the key value pair from main object
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
Without loadash do with Array#shift
and Array#foreach
- First convert obj to array using
Object.keys
- Then loop the value .And remove the first index of array using
Array#shift
- Then apply condition with array length is 0 remove the key value pair from main object
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
var obj = "8": [ "a": true, "b": "xyz": 1 , "a": false, "b": "xyz": 2 ], "13": [ "b": "xyz": 4 ] ;
Object.keys(obj).forEach(a =>
obj[a].shift()
obj[a] = obj[a];
if(obj[a].length == 0)
delete obj[a];
);
console.log(obj)
edited Mar 21 at 17:23
answered Mar 21 at 17:17
prasanthprasanth
14.5k21437
14.5k21437
This is not the desired output
– mhodges
Mar 21 at 17:19
1
@mhodges now check
– prasanth
Mar 21 at 17:21
Looks good now. You could save yourself theshift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.
– mhodges
Mar 21 at 17:22
add a comment |
This is not the desired output
– mhodges
Mar 21 at 17:19
1
@mhodges now check
– prasanth
Mar 21 at 17:21
Looks good now. You could save yourself theshift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.
– mhodges
Mar 21 at 17:22
This is not the desired output
– mhodges
Mar 21 at 17:19
This is not the desired output
– mhodges
Mar 21 at 17:19
1
1
@mhodges now check
– prasanth
Mar 21 at 17:21
@mhodges now check
– prasanth
Mar 21 at 17:21
Looks good now. You could save yourself the
shift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.– mhodges
Mar 21 at 17:22
Looks good now. You could save yourself the
shift()
if you check length at the start of the loop, like mine does. It's an extremely small optimization, though.– mhodges
Mar 21 at 17:22
add a comment |
Use lodash's _.flow()
with _.partialRight()
to create a function that maps the values to the tail (all items but the 1st) of each array, and then uses _.omitBy()
to remove empty keys:
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
add a comment |
Use lodash's _.flow()
with _.partialRight()
to create a function that maps the values to the tail (all items but the 1st) of each array, and then uses _.omitBy()
to remove empty keys:
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
add a comment |
Use lodash's _.flow()
with _.partialRight()
to create a function that maps the values to the tail (all items but the 1st) of each array, and then uses _.omitBy()
to remove empty keys:
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
Use lodash's _.flow()
with _.partialRight()
to create a function that maps the values to the tail (all items but the 1st) of each array, and then uses _.omitBy()
to remove empty keys:
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the terser lodash/fp version:
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
const flow, partialRight: pr, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
pr(mapValues, tail),
pr(omitBy, isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
const flow, mapValues, tail, omitBy, isEmpty = _
const fn = flow(
mapValues(tail),
omitBy(isEmpty)
)
const data = "8":["a":true,"b":"xyz":1,"a":false,"b":"xyz":2],"13":["b":"xyz":4]
const result = fn(data)
console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
answered Mar 21 at 17:19
Ori DroriOri Drori
81.3k138997
81.3k138997
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%2f55285775%2fjavascript-data-transformation%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
you can remove the first element of an array with array.shift
– Chris Li
Mar 21 at 17:11
Thanks for your quick response but how about above input and output. I need to remove first element of each key and return same object. If only 1 element then need to remove that element itself.
– Jwalin Shah
Mar 21 at 17:16
Anybody help me on this?
– Jwalin Shah
Mar 21 at 19:35