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?










1















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?










share|improve this question



















  • 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















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?










share|improve this question



















  • 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








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












3 Answers
3






active

oldest

votes


















1














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)








share|improve this answer

























  • "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











  • @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


















2














Without loadash do with Array#shift and Array#foreach



  1. First convert obj to array using Object.keys

  2. Then loop the value .And remove the first index of array using Array#shift

  3. 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)








share|improve this answer

























  • 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 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



















0














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>








share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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)








    share|improve this answer

























    • "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











    • @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















    1














    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)








    share|improve this answer

























    • "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











    • @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













    1












    1








    1







    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)








    share|improve this answer















    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)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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












    • @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











    • 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












    • @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













    2














    Without loadash do with Array#shift and Array#foreach



    1. First convert obj to array using Object.keys

    2. Then loop the value .And remove the first index of array using Array#shift

    3. 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)








    share|improve this answer

























    • 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 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
















    2














    Without loadash do with Array#shift and Array#foreach



    1. First convert obj to array using Object.keys

    2. Then loop the value .And remove the first index of array using Array#shift

    3. 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)








    share|improve this answer

























    • 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 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














    2












    2








    2







    Without loadash do with Array#shift and Array#foreach



    1. First convert obj to array using Object.keys

    2. Then loop the value .And remove the first index of array using Array#shift

    3. 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)








    share|improve this answer















    Without loadash do with Array#shift and Array#foreach



    1. First convert obj to array using Object.keys

    2. Then loop the value .And remove the first index of array using Array#shift

    3. 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)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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


















    • 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 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

















    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












    0














    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>








    share|improve this answer



























      0














      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>








      share|improve this answer

























        0












        0








        0







        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>








        share|improve this answer













        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>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 at 17:19









        Ori DroriOri Drori

        81.3k138997




        81.3k138997



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript