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?










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.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 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.4k138997




        81.4k138997



























            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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현