const arr = [ { id: 1, date: "2016-05-02", name: "王小虎", address: "上海市普陀区金沙江路 1518 弄" }, { id: 2, date: "2016-05-04", name: "王小虎", address: "上海市普陀区金沙江路 1517 弄" }, { id: 3, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄", children: [ { id: 31, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄", }, { id: 32, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄" }, { id: 33, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄" } ] }, { id: 4, date: "2016-05-03", name: "王小虎", address: "上海市普陀区金沙江路 1516 弄" }];
以上数组根据id分成两个数组
const condition = { 2: [], 3: [31, 33] }// 分成const arr1 = [ { id: 1, date: "2016-05-02", name: "王小虎", address: "上海市普陀区金沙江路 1518 弄" }, { id: 3, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄", children: [ { id: 32, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄" }, ] }, { id: 4, date: "2016-05-03", name: "王小虎", address: "上海市普陀区金沙江路 1516 弄" }];const arr2 = [ { id: 2, date: "2016-05-04", name: "王小虎", address: "上海市普陀区金沙江路 1517 弄" }, { id: 3, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄", children: [ { id: 31, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄", }, { id: 33, date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄" } ] }]
请问应该怎么解决?
const arr = [...]const condition: Record<number, number[]> = { 2: [], 3: [31, 33] }const r = arr.reduce((p, c) => { if (c.id in condition) { if (c.children) { const [a, b] = c.children.reduce( ( [a, b], cc, ) => (condition[c.id].includes(cc.id) ? [a, [...b, cc]] : [[...a, cc], b]), [[], []] as typeof arr[], ) if (a.length) p[0].push({ ...c, children: a }) p[1].push({ ...c, children: b }) } else p[1].push(c) } else p[0].push(c) return p}, [[], []] as typeof arr[])console.log(r)
问题内容: 想象一下,我有一个这样的JS数组: 我想要的是将该数组拆分为N个较小的数组。例如: 对于Python,我有这个: 对于JS,我可以提出的最佳解决方案是递归函数,但我不喜欢它,因为它既复杂又丑陋。这个内部函数返回一个像这样的数组[1,2,3,null,4,5,6,null,7,8],然后我必须再次循环并手动拆分它。(我的第一次尝试是返回此:[1、2、3,[4、5、6,[7、8、9]]],
问题内容: 我有两个像 我希望它们合并成单个数组 问题答案: 您需要该方法。
递归树组,父节点selected为true且partiallySelected为fasle的时候,所有子节点childrens下的节点的selected则也修改为true syncChildrenStatus(arr)后 希望得到的
输出为真或假的两个不同的数组。用函数this my code比较两个数组:
我有一些数据,其中每个id由不同的类型测量,这些类型可以有不同的值。测量值为val。一个小的虚拟数据如下所示: 那么df是: 我需要传播/投射数据,以便每个id的
正如标题所示,我想将两个数组串联成一个更大的数组。例如: 一切顺利。我用的方法“专业”吗?}