使用下划线获取所有键和每个键的惟一值列表
Using underscore to get all keys and list of unique values for each
从一个对象数组开始,我需要得到一个包含所有键和每个键的惟一值的列表。问题是我事先不知道钥匙。如果知道键,有很多解决方案,但在这种情况下,每个对象可以有任意数量的键,每个键都有一个值数组。下面的代码可以工作,但它相当复杂,必须有一个更简单的解决方案。
在这里创建了一个工作的JSBIN
输入:[
{
key_1: [ attribute_value_1, attribute_value_2, ... ],
key_2: [ attribute_value_3, attribute_value_4, ... ],
},
...
]
输出:[
{
label: key_1,
options: [ attribute_value_1, attribute_value_2, ... ]
},
{
label: key_2,
options: [ attribute_value_3, attribute_value_4, ... ]
},
...
]
建议解决方案:
_.chain(input)
.map(function (attr) {
return _.keys(attr).map(function (key) {
return {
key: key,
value: attr[key]
};
});
})
.flatten()
.groupBy('key')
.map(function (grouped_values, key) {
// value = array of { key, value }
return {
label: key,
options: _.chain(grouped_values)
.pluck('value')
.flatten()
.uniq()
.value()
};
})
.value();
Using lodash -将_.mergeWith()
应用于输入数组,并使用customizer函数将数组组合并获得唯一值。然后_.map()
将结果转换为所需格式:
var input = [
{
key_1: [ "attribute_value_1", "attribute_value_2" ],
key_2: [ "attribute_value_3", "attribute_value_4" ]
},
{
key_1: [ "attribute_value_1", "attribute_value_5" ],
key_2: [ "attribute_value_2", "attribute_value_3" ],
key_5: [ "attribute_value_2", "attribute_value_3" ]
}
];
var params = [{}].concat(input).concat(function (objValue, srcValue) { // create the params to apply to mergeWith
if (_.isArray(objValue)) {
return _.union(objValue, srcValue); // merge the arrays, and get the unique values
}
});
var result = _.map(_.mergeWith.apply(_, params), function(value, key) { // merge all objects in the array, and map the results to required format
return {
label: key,
options: value
};
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.2/lodash.min.js"></script>
如果你使用ES6,你可以清理难看的params
数组:
const input = [
{
key_1: [ "attribute_value_1", "attribute_value_2" ],
key_2: [ "attribute_value_3", "attribute_value_4" ]
},
{
key_1: [ "attribute_value_1", "attribute_value_5" ],
key_2: [ "attribute_value_2", "attribute_value_3" ],
key_5: [ "attribute_value_2", "attribute_value_3" ]
}
];
const customizer = (objValue, srcValue) => {
if (_.isArray(objValue)) {
return _.union(objValue, srcValue); // merge the arrays, and get the unique values
}
};
const result = _.map(_.mergeWith({}, ...input, customizer), (value, key) => ({ // merge all objects in the array, and map the results to required format
label: key,
options: value
}));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.2/lodash.min.js"></script>
如果您喜欢的话,可以用下划线来写,但是没有必要这么做。
const input = [
{key_1: [1, 2], key_2: [3, 4]},
{key_3: [5, 6], key_2: [7, 42]}
];
var result = [].concat(...input.map(obj =>
Object.keys(obj).map(key =>
({label: key, options: obj[key]})
)
));
console.log(result);
我不太确定你想如何统一结果。您的意思是您希望在每个数组(如[ attribute_value_1, attribute_value_2, ... ]
)中运行唯一的吗?
相关文章:
- es6 相当于下划线查找位置
- 用空格替换下划线PHP
- 如何使用下划线js转换这些数据
- 如何保持引导程序下拉复选框列表下拉
- 带嵌套json的下划线js查找
- 如何逃离<>在javascript下划线模板中
- 下划线,将对象列表转换为对象值数组
- 如何删除列表中的对象?Javascript nodejs和下划线
- 从剑道下拉列表中删除下划线
- 检查列表是否在javascript中使用下划线.js排序
- 如何在具有某些条件的情况下从列表中删除重复项,下划线.js
- 下划线.js:使用对象中找到的键从对象列表中创建映射
- 下划线模板,将列表添加到单个数组中
- 使用下划线从对象列表中筛选匹配的值
- 电子邮件列表/集合使用下划线/短划线进行混洗和配对
- 使用下划线获取所有键和每个键的惟一值列表
- 使用groupBy下划线分组嵌套列表
- . net WebApp中的下拉列表-是否可以在列表中的一个或两个项目下划下划线?
- 使用洛达什下划线比较重复电子邮件的列表
- j查询或下划线 按范围文本对列表进行排序