简体   繁体   中英

Massaging JSON Data with underscore.js

I have stored the data in the following manner

[
  {
    "metadata": {
      "subtitle": "kit",
      "id": "0063400000yCqRfAAK",
      "title": "#9864478"
    },
    "entityTitle": "Opportunities"
  },
  {
    "metadata": {
      "subtitle": "Acceleration",
      "id": "0063400000yCqRfAAK",
      "title": "#9882278"
    },
    "entityTitle": "Reports"
  }
]

I need to transform this into :

[
  {
    "entityTitle": "Opportunities",
    "searchResults": [
      {
        "subtitle": "kit",
        "id": "0063400000yCqRfAAK",
        "title": "#9864478"
      }
    ]
  },
  {
    "entityTitle": "Reports",
    "searchResults": [
      {
        "subtitle": "Acceleration",
        "id": "0063400000yCqRfAAK",
        "title": "#9882278"
      }
    ]
  }
]

I have used _.groupBy , like this

_.groupBy(recentData, function(data) {
            return data.entityTitle;
});


and I got :

{
  "Opportunities": [
    {
      "metadata": {
        "subtitle": "kit",
        "id": "0063400000yCqRfAAK",
        "title": "#9864478"
      },
      "entityTitle": "Opportunities"
    }
  ],
  "Reports": [
    {
      "metadata": {
        "subtitle": "Acceleration",
        "id": "0063400000yCqRfAAK",
        "title": "#9882278"
      },
      "entityTitle": "Reports"
    }
  ]
}

I need to store the recently searched data into an array, so I am storing the data like this.
Can anyone help please me get the data into proper format?

Just a simple mapping will suffice:

 const data = [ { "metadata": { "subtitle": "kit", "id": "0063400000yCqRfAAK", "title": "#9864478" }, "entityTitle": "Opportunities" }, { "metadata": { "subtitle": "Acceleration", "id": "0063400000yCqRfAAK", "title": "#9882278" }, "entityTitle": "Reports" } ]; const transformation = data.map( item => ({ "entityTitle": item.entityTitle, "searchResults": [ item.metadata ] })); console.log( transformation ); 

But I would advice against storing the search results this way. I would store the current search results in a seperate array and just have the indexes of the entity inside the entities array, so that there's a clear seperation between the searches and the entities, since the entity should not care about if it's being searched or not.

In case there's multiple entities of the same name, we can replace the mapping by a reduction:

 const data = [ { "metadata": { "subtitle": "kit", "id": "0063400000yCqRfAAK", "title": "#9864478" }, "entityTitle": "Opportunities" }, { "metadata": { "subtitle": "Acceleration", "id": "0063400000yCqRfAAK", "title": "#9882278" }, "entityTitle": "Reports" }, { "metadata": { "subtitle": "kat", "id": "0045100000yCqRfAAK", "title": "#98640100" }, "entityTitle": "Opportunities" } ]; const transformation = Object.values( data.reduce(( groups, item ) => { const entityTitle = item.entityTitle; if ( !groups[ entityTitle ] ) groups[ entityTitle ] = { entityTitle, "searchResults": [] }; groups[ entityTitle ].searchResults.push( item.metadata ); return groups; }, {} ) ); console.log( transformation ); 

Use a chain and map the groups created by _groupBy. The search results within each group also need mapping to remove the metadata property

 let res = _.chain(data) .groupBy('entityTitle') .map(function(results, title) { return {entityTitle: title, searchResults: _.map(results, 'metadata') }; }) .value(); console.log(res) 
 .as-console-wrapper { max-height: 100%!important; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> <script> data = [{ "metadata": { "subtitle": "kit", "id": "0063400000yCqRfAAK", "title": "#9864478" }, "entityTitle": "Opportunities" }, { "metadata": { "subtitle": "foo", "id": "foo", "title": "#foo" }, "entityTitle": "Opportunities" }, { "metadata": { "subtitle": "Acceleration", "id": "0063400000yCqRfAAK", "title": "#9882278" }, "entityTitle": "Reports" } ] </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