数组json数据重组
作者:互联网
var arr = [ {value: "颜色", detail: ["红"]}, {value: "尺寸", detail: ["29cm", "30cm"]}]
let result = arr.reduce((res, item) => { return item.detail.reduce((list, ci) => { let nobj = {[item.value]: ci} let narr = res.length ? res.map(r => ({...r, ...nobj})) : [nobj] return list.concat(narr) }, []) }, [])
console.log(result)
result = [ {颜色:红, 尺寸: 29cm}, {颜色:红, 尺寸: 30cm}, ]
笛卡尔乘积
标签:重组,res,detail,value,item,json,result,数组,nobj 来源: https://www.cnblogs.com/yanghao-blogs/p/15503414.html