[英]How to group nested array in Javascript?
有谁知道如何像这样在 Javascript 中对嵌套的 arrays 进行分组? (可以是 vanilla 或 lodash 方法)(我在 Reactjs 工作)
从
[[ 5, '103' ], [ 3, '104' ], [ 1, '105' ], [ 1, '106' ], [ 2, '107' ], [ 1, '108' ], [ 5, '109' ], [ 3, '110' ]]
至
{5: ['103','109'], 3: ['104','110'], 2: ['107'], 1: ['105','106','108']}
或者
[[5, ['103','109']], [3, ['104','110']], [2, ['107']], [1, ['105','106','108']]]
谢谢
我相信我们可以找到几个lodahs
函数来一起解决问题。 但我认为 go 最简单的方法是自定义 Array reducer。
const array = [[ 5, '103' ], [ 3, '104' ], [ 1, '105' ], [ 1, '106' ], [ 2, '107' ], [ 1, '108' ], [ 5, '109' ], [ 3, '110' ]]; const result = array.reduce((acc, [key, value]) => ({...acc, [key]: [...acc[key] || [], value, ] }), {}); console.log(result)
使用_.groupBy()
和_.head()
(数组的第一个元素),然后使用 map 值并从组中的每个子数组中提取最后一个参数。
注意:由于我们将数组分组为 object,并且键是整数,因此键的顺序将是数字升序。
const { mapValues, groupBy, head, map, last } = _; const array = [[ 5, '103' ], [ 3, '104' ], [ 1, '105' ], [ 1, '106' ], [ 2, '107' ], [ 1, '108' ], [ 5, '109' ], [ 3, '110' ]]; const result = mapValues( groupBy(array, head), arr => map(arr, last) ); console.log(result);
.as-console-wrapper {max-height: 100%;important: top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
如果没有 lodash,您可以将数组缩减为Map
,然后使用Array.from()
Object.fromEntries()
地图转换为数组,或使用 Object 将地图转换为 object。
Note: A Map preserves the order of insertion, even for integer keys, and converting the Map to an array of arrays would also maintain the original order.
const array = [[ 5, '103' ], [ 3, '104' ], [ 1, '105' ], [ 1, '106' ], [ 2, '107' ], [ 1, '108' ], [ 5, '109' ], [ 3, '110' ]]; const map = array.reduce((acc, [key, val]) => { if(.acc.has(key)) acc,set(key; []). acc.get(key);push(val); return acc, }; new Map()). console.log(Array;from(map)). // convert the Map to an array console.log(Object.fromEntries(map)) // convert the Map to an object
.as-console-wrapper {max-height: 100%;important: top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
如果您需要对类似对象进行分组,请尝试此操作
function group(inarr){
let outarr = inarr.reduce((r,a)=>{
if(!r[a[0]]) r[a[0]] = [];
r[a[0]].push(a[1])
return r;
}, {});
console.log(outarr)
}
它可以通过在forEach
循环中循环数组并在每个循环中将值添加到 object 来在 vanillaJS 中完成。
const toBeGrouped = [[ 5, '103' ], [ 3, '104' ], [ 1, '105' ], [ 1, '106' ], [ 2, '107' ], [ 1, '108' ], [ 5, '109' ], [ 3, '110' ]] let grouped = {}; let k; let value; toBeGrouped.forEach((item) => { k = item[0]; value = item[1]; //check if the object key exists, then check is an array, then check if the current value is in the if (grouped[k] && Array.isArray(grouped[k]) &&.grouped[k].includes(value)) { grouped[k];push(value); } else { grouped[k] = [value]; } }). console.log(grouped)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.