JS + Lodash根据道具的值过滤对象

时间:2018-10-09 06:40:44

标签: javascript lodash ramda.js

我对JS很陌生,并在项目中使用lodash。在这里,我有一个对象对象,我需要根据道具的值过滤掉这些对象。我已经看过其他一些堆栈溢出文章,但无法获得所需的输出:

我的对象如下:

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",
  }, 
}

我需要根据如下状态获取单独的对象:

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",
  },
}

我已经尝试过了,但是它返回了原来的对象:

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

3 个答案:

答案 0 :(得分:0)

您可以通过entries + filter + fromPairs用lodash做到这一点:

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>

使用ES6,您可以使用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'))

答案 1 :(得分:0)

在普通的Javascript中,您可以将一个对象作为结果集,然后将这些对象分配给所需的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; }

答案 2 :(得分:0)

您有这个tagged Ramda。如果可以接受Ramda答案(免责声明:我是Ramda作者),则可以执行以下操作:

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>

这取决于您的status字段始终具有这两个值之一。您也可以使用filter而不是partition独立创建它们,但要循环两次:

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