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?
Is a distribution that is normal, but highly skewed considered Gaussian?
What steps are necessary to read a Modern SSD in Medieval Europe?
Using Rolle's theorem to show an equation has only one real root
Where do students learn to solve polynomial equations these days?
Why do airplanes bank sharply to the right after air-to-air refueling?
Are police here, aren't itthey?
Why, when going from special to general relativity, do we just replace partial derivatives with covariant derivatives?
Axiom Schema vs Axiom
Does increasing your ability score affect your main stat?
Unclear about dynamic binding
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
Why does standard notation not preserve intervals (visually)
Why isn't the Mueller report being released completely and unredacted?
How to check if all elements of 1 list are in the *same quantity* and in any order, in the list2?
Legal workarounds for testamentary trust perceived as unfair
Do I need to write [sic] when a number is less than 10 but isn't written out?
Dominated convergence theorem - what sequence?
Why is information "lost" when it got into a black hole?
Do they change the text of the seder in Israel?
Is micro rebar a better way to reinforce concrete than rebar?
Is the D&D universe the same as the Forgotten Realms universe?
A Man With a Stainless Steel Endoskeleton (like The Terminator) Fighting Cloaked Aliens Only He Can See
Is wanting to ask what to write an indication that you need to change your story?
Is it possible to use a NPN BJT as switch, from single power source?
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.1k62645
12.1k62645
"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.4k138997
81.4k138997
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