使用下划线获取所有键和每个键的唯一值列表
Using underscore to get all keys and list of unique values for each
从一组对象开始,我需要获取所有键的列表以及每个键的所有唯一值。问题是我事先不知道钥匙。如果您知道密钥,则有很多解决方案,但在这种情况下,每个对象都可以有任意数量的密钥,并且每个密钥都有一个值数组。下面的代码可以工作,但是它很复杂,必须有一个更简单的解决方案。
输入:
[
{
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();
使用 lodash - 将结果应用 _.mergeWith()
to the input array, and use the customizer function to combine the arrays and get the unique values. Afterwards _.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>
如果你愿意,你可以用 Underscore 写这个,但没有迫切需要这样做。
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, ... ]
?
从一组对象开始,我需要获取所有键的列表以及每个键的所有唯一值。问题是我事先不知道钥匙。如果您知道密钥,则有很多解决方案,但在这种情况下,每个对象都可以有任意数量的密钥,并且每个密钥都有一个值数组。下面的代码可以工作,但是它很复杂,必须有一个更简单的解决方案。
输入:
[
{
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();
使用 lodash - 将结果应用 _.mergeWith()
to the input array, and use the customizer function to combine the arrays and get the unique values. Afterwards _.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>
如果你愿意,你可以用 Underscore 写这个,但没有迫切需要这样做。
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, ... ]
?