简体   繁体   中英

JS + Lodash Filter an object on the basis of the value of a prop

I'm pretty new to JS and using lodash for a project. Here I have an object of objects and I need to filter out the objects according to the value of a prop. I've gone through some other stack overflow post but did not able to get the required output :

My object is as follows :

var parameters = {
  "param_a": {
    "created": "2018-10-08T05:14:19.498Z", 
    "description": "Description of the Parameter A", 
    "status": "absolute",
  }, 
  "param_b": {
    "created": "2018-10-08T05:14:19.498Z", 
    "description": "Description of the Parameter B", 
    "status": "absolute",
  },
  "param_c": {
    "created": "2018-10-08T05:14:19.498Z", 
    "description": "Description of the Parameter C", 
    "status": "relative",
  }, 
}

I need to get separate objects according to a status like :

var absolute_parameters = {
  "param_a": {
    "created": "2018-10-08T05:14:19.498Z", 
    "description": "Description of the Parameter A", 
    "status": "absolute",
  }, 
  "param_b": {
    "created": "2018-10-08T05:14:19.498Z", 
    "description": "Description of the Parameter B", 
    "status": "absolute",
  },
}

var relative_parameters = {
  "param_c": {
    "created": "2018-10-08T05:14:19.498Z", 
    "description": "Description of the Parameter C", 
    "status": "relative",
  },
}

I've tried this but it returns the original object as it is :

 relative_parameters = _.pickBy(parameters, function(value, key) {return 'relative';})

You could do it with lodash via entries + filter + fromPairs :

 var parameters = { "param_a": { "created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter A", "status": "absolute", }, "param_b": { "created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter B", "status": "absolute", }, "param_c": { "created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter C", "status": "relative", }, } const getByStatus = (s) => _(parameters).entries() .filter(([k,{status}]) => _.isEqual(status,s)).fromPairs().value() console.log(getByStatus('absolute')) console.log(getByStatus('relative')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script> 

With ES6 you could use Object.entries + Array.filter + Array.reduce :

 var parameters = { "param_a": { "created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter A", "status": "absolute", }, "param_b": { "created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter B", "status": "absolute", }, "param_c": { "created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter C", "status": "relative", }, } const getByStatus = (s) => Object.entries(parameters) .filter(([k,{status}]) => status == s).reduce((r,[k,v]) => (r[k]=v,r),{}) console.log(getByStatus('absolute')) console.log(getByStatus('relative')) 

In plain Javascript, you could take an object as result set and assign the objects to the wanted group of status

 var data = { param_a: { created: "2018-10-08T05:14:19.498Z", description: "Description of the Parameter A", status: "absolute" }, param_b: { created: "2018-10-08T05:14:19.498Z", description: "Description of the Parameter B", status: "absolute" }, param_c: { created: "2018-10-08T05:14:19.498Z", description: "Description of the Parameter C", status: "relative" } }, absolute_parameters = {}, relative_parameters = {}; Object .entries(data) .reduce( (r, [k, o]) => (Object.assign(r[o.status], { [k]: o }), r), { absolute: absolute_parameters, relative: relative_parameters } ); console.log(absolute_parameters); console.log(relative_parameters); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You have this tagged Ramda . If a Ramda answer is acceptable (disclaimer: I'm a Ramda author), you could do something like this:

 const parameters = {"param_a": {"created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter A", "status": "absolute"}, "param_b": {"created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter B", "status": "absolute"}, "param_c": {"created": "2018-10-08T05:14:19.498Z", "description": "Description of the Parameter C", "status": "relative"}} const [absolute_parameters, relative_parameters] = R.partition(R.propEq('status', 'absolute'), parameters) console.log(absolute_parameters) console.log(relative_parameters) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script> 

This depends upon your status field always having one of these two values. You could also create them independently, using filter instead of partition , at the cost of looping twice:

const absolute_parameters = R.filter(R.propEq('status', 'absolute'), parameters)
const relative_parameters = R.filter(R.propEq('status', 'relative'), parameters)

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