简体   繁体   中英

How to use lodash to remove keys from a JSON object?

I've been trying to transform some data using lodash without success. I am really new to javascript and lodash. How can I get the expected result? I've used mapValues and chain , but I didn't achieve anything good.

const data = {
  "north": [
    {
      "2018-07-01": {
        "date": "2018-07-01",
        "name": "david",
        "age": 11
      },
      "2018-07-02": {
        "date": "2018-07-02",
        "name": "damo",
        "age": 16
      },
      "2018-07-03": {
        "date": "2018-07-03",
        "name": "dani",
        "age": 12
      }
    }
  ],
  "south": [
    {
      "2018-07-01": [
        {
          "fruit": "banana",
          "date": "2018-07-01",
          "name": "miller",
          "age": 11
        },
        {
          "fruit": "mango",
          "date": "2018-07-01",
          "name": "mano",
          "age": 11
        },
        {
          "fruit": "avocado",
          "date": "2018-07-01",
          "name": "karl",
          "age": 14
        }
      ],
      "2018-07-02": [
        {
          "fruit": "pineaplle",
          "date": "2018-07-02",
          "name": "gautier",
          "age": 12
        },
        {
          "fruit": "apple",
          "date": "2018-07-02",
          "name": "gauteng",
          "age": 9
        },
        {
          "fruit": "watermelon",
          "date": "2018-07-02",
          "name": "garzier",
          "age": 12
        }
      ]
    }
  ]
};

Below is the expected result. I am trying to remove the dates which are outside the objects and arrays.

const expectedData = {
  "north": [
    {
      "date": "2018-07-01",
      "name": "david",
      "age": 11
    },
    {
      "date": "2018-07-02",
      "name": "damo",
      "age": 16
    },
    {
      "date": "2018-07-03",
      "name": "dani",
      "age": 12
    }
  ],
  "south": [
    {
      "fruit": "banana",
      "date": "2018-07-01",
      "name": "miller",
      "age": 11
    },
    {
      "fruit": "mango",
      "date": "2018-07-01",
      "name": "mano",
      "age": 11
    },
    {
      "fruit": "avocado",
      "date": "2018-07-01",
      "name": "karl",
      "age": 14
    },
    {
      "fruit": "pineaplle",
      "date": "2018-07-02",
      "name": "gautier",
      "age": 12
    },
    {
      "fruit": "apple",
      "date": "2018-07-02",
      "name": "gauteng",
      "age": 9
    },
    {
      "fruit": "watermelon",
      "date": "2018-07-02",
      "name": "garzier",
      "age": 12
    }
  ]
};

You don't really need lodash for this. You can look at each key in your data and the just pull the values from each element of the array and concat it into a new array.

 const data = {"north": [{"2018-07-01": {"date": "2018-07-01","name": "david","age": 11},"2018-07-02": {"date": "2018-07-02","name": "damo","age": 16},"2018-07-03": {"date": "2018-07-03","name": "dani","age": 12}}],"south": [{"2018-07-01": [{"fruit": "banana","date": "2018-07-01","name": "miller","age": 11},{"fruit": "mango","date": "2018-07-01","name": "mano","age": 11},{"fruit": "avocado","date": "2018-07-01","name": "karl","age": 14}],"2018-07-02": [{"fruit": "pineaplle","date": "2018-07-02","name": "gautier","age": 12},{"fruit": "apple","date": "2018-07-02","name": "gauteng","age": 9},{"fruit": "watermelon","date": "2018-07-02","name": "garzier","age": 12}]}]}; Object.keys(data).forEach(k => { data[k] = data[k].reduce((a, c) => a.concat(...Object.values(c)), []) }) console.log(data) 

This starts with each key in your original object north and south . And for each one replaces the array with the accumulated values of each object in that array ignoring the keys.

alternatively you could just do this

const expectedData = {
    north: Object.values(data.north[0]),
    south: Object.values(data.south[0])
}

You can do any lodash quick operations, in pure JS way. But, since you tagged lodash here is the version:

_.mapValues(data, v => _.flatMapDeep(v, _.values))

 var data = { "north": [ { "2018-07-01": { "date": "2018-07-01", "name": "david", "age": 11 }, "2018-07-02": { "date": "2018-07-02", "name": "damo", "age": 16 }, "2018-07-03": { "date": "2018-07-03", "name": "dani", "age": 12 } } ], "south": [ { "2018-07-01": [ { "fruit": "banana", "date": "2018-07-01", "name": "miller", "age": 11 }, { "fruit": "mango", "date": "2018-07-01", "name": "mano", "age": 11 }, { "fruit": "avocado", "date": "2018-07-01", "name": "karl", "age": 14 } ], "2018-07-02": [ { "fruit": "pineaplle", "date": "2018-07-02", "name": "gautier", "age": 12 }, { "fruit": "apple", "date": "2018-07-02", "name": "gauteng", "age": 9 }, { "fruit": "watermelon", "date": "2018-07-02", "name": "garzier", "age": 12 } ] } ] }; var expectedData = _.mapValues(data, v => _.flatMapDeep(v, _.values)); console.log(expectedData); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM