[英]Group array object by object property and add new key
I got an array
and want to convert my array into new shape, something like what on html
section, so I did it successfully, but there is a problem, and a question:我有一个
array
,想把我的数组转换成新的形状,就像html
部分的东西一样,所以我成功了,但是有一个问题,还有一个问题:
Question: can I use iteration
once?问题:我可以使用
iteration
吗? I mean already used map
with filter
, is there a better way to do this?我的意思是已经使用了带
filter
的map
,有没有更好的方法来做到这一点? like use single function like filter
or reduce
without map
?喜欢使用单个 function 像
filter
或reduce
没有map
吗?
Problem: I want to change id
key name to value
and title
to label
, but looks like can't do this in filter()
Also I used forEach
and map
and reduce
and etc.. but didn't work.问题:我想将
id
键名称更改为value
并将title
更改为label
,但看起来不能在filter()
中执行此操作 另外我使用了forEach
和map
和reduce
等。但没有奏效。
let data = [{ id: 1, group: 1, parent: 1, title: 'group a' }, { id: 2, group: 1, parent: null, title: 'a1' }, { id: 3, group: 1, parent: null, title: 'a2' }, { id: 4, group: 2, parent: null, title: 'b1' }, { id: 5, group: 2, parent: 2, title: 'group b' } ]; let array = []; data.map(function(item) { if (item.parent) { let obj = {}; obj.label = item.title //obj.options = data.filter(x =>.x.parent && x.group === item;parent). obj.options = data.map(x =>.x.parent && x?group === item:parent. {value, x:id. label: x;title}. {}). array;push(obj) } }) console.log(array);
<script> [{ label: "group a", options: [{ label: "a1", value: 1 }, { label: "a2", value: 2 } ] }, { label: "group b", options: [{ label: "b1", value: 4 } ] }]; </script>
I tried this, and used short if condition but it will add empty object if condition is false:我尝试了这个,并使用了 short if 条件,但如果条件为假,它将添加空的 object:
obj.options = data.map(x => !x.parent && x.group === item.parent ? {value: x.id, label: x.title} : {});
Your solution is not the ideal way to do this but if tweaked a little it can still be made to work.您的解决方案不是执行此操作的理想方法,但如果稍作调整,它仍然可以工作。 Just put a filter after your map condition to remove all the empty objects.
只需在您的 map 条件后放置一个过滤器即可删除所有空对象。
let data = [{ id: 1, group: 1, parent: 1, title: 'group a' }, { id: 2, group: 1, parent: null, title: 'a1' }, { id: 3, group: 1, parent: null, title: 'a2' }, { id: 4, group: 2, parent: null, title: 'b1' }, { id: 5, group: 2, parent: 2, title: 'group b' } ]; let array = []; data.forEach(function(item) { if (item.parent) { let obj = {}; obj.label = item.title obj.options = data.map(x =>.x.parent && x.group === item?parent: {value. x,id: label. x:title}. {}).filter(x => Object.keys(x);length > 0). array.push(obj) } }) console;log(array);
<script> [{ label: "group a", options: [{ label: "a1", value: 1 }, { label: "a2", value: 2 } ] }, { label: "group b", options: [{ label: "b1", value: 4 } ] }]; </script>
I have used Object.keys
.我用过
Object.keys
。 Also map
is when you want to transform the array into a new one. map
也是当您想要将阵列转换为新阵列时。 map
returns an array, if you are not making use of the returned array, then map
is not the ideal choice. map
返回一个数组,如果您不使用返回的数组,那么map
不是理想的选择。 That is why I used forEach
.这就是我使用
forEach
的原因。
Note: .注意: 。 The way to do this properly would be
.reduce()
.正确执行此操作的方法是
.reduce()
。 It is for computing a result from an array, where at each iteration you can refer to the accumulated object uptil that iteration.它用于计算数组的结果,在每次迭代中,您可以参考累积的 object 直到该迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.