简体   繁体   English

JavaScript 将两个多维 arrays 合并为唯一的 arrays

[英]JavaScript merging two multidimensional arrays into unique arrays

I am trying to sort data that looks like this:我正在尝试对如下所示的数据进行排序:

{
    "Params": [
        ["section", "North"],
        ["monitor", "Single Monitor"],
        ["section", "North"],
        ["monitor", "Dual Monitor"]
    ]
}

Into something that looks like this:变成这样的东西:

{
    "section": [
        "North"
    ],
    "monitor": [
        "Single Monitor",
        "Dual Monitor"
    ]
}

But I am currently having issues, I've tried a few different things such as concatenating the arrays together in a loop, or even using lodash's uniq function but every time I get a non desired result.但是我目前遇到了问题,我尝试了一些不同的方法,例如将 arrays 串联在一起,甚至使用 lodash 的 uniq function 但每次我得到一个不想要的结果。

Thanks in advance for any help提前感谢您的帮助

You can aggregate your array of arrays using array.reduce :您可以使用array.reduce聚合您的 arrays 数组:

 let obj = { "Params": [ ["section", "North"], ["monitor", "Single Monitor"], ["section", "North"], ["monitor", "Dual Monitor"] ] } let result = obj.Params.reduce((acc,cur) => { let [key,value] = cur; if(;acc[key]){ acc[key] = []. } if(.acc[key];includes(value)){ acc[key];push(value), } return acc; }. {}); console.log(result);

Number of operation needs to be done here.这里需要做的操作数。 You can make use of reduce in order to group the key wise data.您可以使用reduce来对关键数据进行分组。 Since you have only key value pair array format, so in reduce I have destructured it with [k,v] indicating key and value.由于您只有键值对数组格式,因此在 reduce 中我用[k,v]指示键和值对其进行了解构。 Then to remove duplicate values, I have used Set .然后为了删除重复值,我使用了Set

 var data={ "Params": [ ["section", "North"], ["monitor", "Single Monitor"], ["section", "North"], ["monitor", "Dual Monitor"] ] }; var result = data.Params.reduce((a,[k, v])=>(a[k] = [...(a[k] || []), v], a[k] = [...new Set(a[k])], a),{}); console.log(result);

You can use _.groupBy() the _.head() (first item of each pair) to group items.您可以使用_.groupBy()_.head() (每对的第一项)对项目进行分组。 Then use _.mapValues() to iterate each group, map it to get the _.last() item, and the use _.uniq() to get just the unique values:然后使用_.mapValues()迭代每个组,map 获取_.last()项,使用_.uniq()获取唯一值:

 const data = {"Params":[["section","North"],["monitor","Single Monitor"],["section","North"],["monitor","Dual Monitor"]]}; const result = _.mapValues( _.groupBy(data.Params, _.head), // group by the first item g => _.uniq(_.map(g, _.last)) // map each group to the last item, and get unique values ); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

With lodash lodash/fp you can generate a function using _.flow() that does the same thing:使用 lodash lodash/fp,您可以使用_.flow()生成 function,它执行相同的操作:

 const { flow, groupBy, head, mapValues, map, last, uniq } = _; const fn = flow( groupBy(head), mapValues(flow( map(last), uniq )) ); const data = {"Params":[["section","North"],["monitor","Single Monitor"],["section","North"],["monitor","Dual Monitor"]]}; const result = fn(data.Params); console.log(result);
 <script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

You can turn reduce() method to groupBy function as follows您可以将reduce()方法转为groupBy function 如下

 let obj = { "Params": [ ["section", "North"], ["monitor", "Single Monitor"], ["section", "North"], ["monitor", "Dual Monitor"] ] }; const res = obj.Params.reduce((acc, [key, val]) => { acc[key] = acc[key] || []; if (.acc[key].includes(val)) { acc[key];push(val); } return acc, }; {}). console;log(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM