Underscore.js - 将键/值对数组映射到对象 - 单行 [英] Underscore.js - Map Array of key/value pairs to an Object - One liner

查看:31
本文介绍了Underscore.js - 将键/值对数组映射到对象 - 单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览 underscore docs 但我似乎找不到一个方法(或嵌套方法调用)进行以下转换:

I've been going through the underscore docs but I can't seem to find a method (or nested method call) to do the following transformation:

假设我有以下 Javascript 数组:

Let's say I have the following Javascript array:

 [{ "name" : "sEcho", "value" : 1},{ "name" : "iColumns", "value" : 12}, ... ]

我需要将其转换为以下对象:

And I need to transform it into the following object:

 {
      sEcho: 1,
      iColumns: 12,
      ...
 }

我使用 underscore.js 是有原因的,所以它必须是一行.

I'm using underscore.js for a reason so it must be a one liner.

推荐答案

由于没有人将此作为答案发布,我发布它是因为我认为它比 简的回答.没有内联函数,它更短、更干净.

Since nobody has posted this as an answer, I'm posting it because I think it is better than Jan's answer. It's shorter, and cleaner without the inline function.

_.object(_.pluck(data, 'name'), _.pluck(data, 'value'));

这篇关于Underscore.js - 将键/值对数组映射到对象 - 单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆