【问题标题】:lo-dash : JSON parsing and returning required outputlo-dash : JSON 解析并返回所需的输出
【发布时间】:2018-01-21 06:18:00
【问题描述】:

我是 lodash 的新手,有以下疑问。我附上了我的一个示例 json。我推荐了许多其他帖子,但对我没有任何帮助

  • 我想找到状态为真的数组并返回以下值

    1. 仅限姓名
    2. 名称和部件号
  • 我也想只返回状态和名称

[{
		'state': true,
		'partno': 21,
		'idno': 1,
		'name': 'abc'
	},
	{
		'state': true,
		'partno': 22,
		'idno': 2,
		'name': 'xyz'
	},
	{
		'state': false,
		'partno': 23,
		'idno': 3,
		'name': 'mnd'
	},
	{
		'state': true,
		'partno': 26,
		'idno': 4,
		'name': 'koi'
	},
	{
		'state': false,
		'partno': 21,
		'idno': 1,
		'name': 'abc'
	}
]

【问题讨论】:

    标签: lodash


    【解决方案1】:

    下面的代码可以满足你的需要

    const data = getData();
    
    console.log('I want to...\n\n');
    
    
    console.log('1. find array with state as true and return name only')
    const result1 = _(data)
      .filter('state')
      .map('name')
      .value();
    
    // logAsJson is a helper function below
    logAsJson(result1);
         
         
    console.log('2. find array with state as true and return name and partno');
    const result2 = _(data)
      .filter('state')
      // pickProperties is a helper function below
      .map(pickProperties(['name', 'partno']))
      .value();
    logAsJson(result2);
    
    
    console.log('3. just return state and name');
    const result3 = _.map(data, pickProperties(['state', 'name']));
    logAsJson(result3);
    
    
    //
    // Helper functions
    //
    
    function logAsJson(anObject) {
      console.log(JSON.stringify(anObject, null, 2));
    }
    
    function pickProperties(propsToPick) {
      return function mapperFn(anObject) {
        return _.pick(anObject, propsToPick);
      };
    }
    
    function getData() {
      return [
        {
          'state': true,
          'partno': 21,
          'idno': 1,
          'name': 'abc'
        },
        {
          'state': true,
          'partno': 22,
          'idno': 2,
          'name': 'xyz'
        },
        {
          'state': false,
          'partno': 23,
          'idno': 3,
          'name': 'mnd'
        },
        {
          'state': true,
          'partno': 26,
          'idno': 4,
          'name': 'koi'
        },
        {
          'state': false,
          'partno': 21,
          'idno': 1,
          'name': 'abc'
        }
      ];
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

    【讨论】:

      【解决方案2】:

      也许我没有得到你的问题,但这是你所要求的:

      //Filters your array and creates a new object
      yourArray.filter(x => x.state)
             .map( x => ({'name':x.name, 'partno':x.partno}))
      

      我不知道 lodash,但是看了一下文档,我得到了这个:

      _map(_.filter(yourArray, 'state'), (x) => ({'name':x.name,'partno':x.partno})
      

      这几乎是一样的。您为此使用 lodash 有什么原因吗?

      【讨论】:

      • 在 lodash 中您可以使用链接:_(yourArray).map(...).filter(...).value()(返回最终输出需要 fn 值)
      • @Z.Khullah 不过,对于您可以使用标准库轻松实现的东西,似乎更麻烦。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 2018-09-30
      • 2014-02-05
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多