-
隔江千里
[ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, ], [ { id: 2 }, { id: 2 }, { id: 3 }, { id: 5 }, ],]function createNewArray(arr, n) { const newArr = []; arr.forEach(element => { newArr.push(element[n]) }); return newArr;}function getResult() { const tempArray = []; for (let index = 0; index < origin[0].length; index++){ const newArray = createNewArray(origin, index).map(e => e.id); if ([...new Set(newArray)].length > 1) { tempArray.push(newArray); } } let result = Array(tempArray[0].length).fill([]); return result.map((item, index) => tempArray.map(e => e[index]));}console.log('result', getResult());
-
茅侃侃
不是一个完美的解决方案,但得到预期的结果:id's两个过滤器13993,13998检查id并相应地修改值const array1 = [ [{ checked: false, id: 13993, type: "checkbox", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "label", }, { checked: false, id: 13940, type: "label", }, ], [{ checked: false, id: 13993, type: "checkbox", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "checkbox", }, { checked: false, id: 13940, type: "label", }, ], [{ checked: false, id: 13993, type: "checkbox", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "checkbox", }, { checked: false, id: 13940, type: "label", }, ], [{ checked: false, id: 13993, type: "label", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "label", }, { checked: false, id: 13940, type: "label", }, ]];let filteredArray = array1.map(x=>x.filter(v=>v.id==13993||v.id==13998)) // 1st steplet result = filteredArray.map(x=>x.filter(v=>v.id==13993?v.type="checkbox":v.type="label")) // 2nd step//2nd step i guess can be done in much efficient wayconsole.log(result)
-
手掌心
你的意思是这样的吗?const removeCol = (table, colIndex) => { table.forEach(row => row.splice(colIndex, 1)) }removeCol(array1, 0); // To remove column 0 (0-indexed)removeCol(array1, 1); // To remove column 1 (0-indexed)
-
摇曳的蔷薇
也许你可以像下面这样尝试let arr = [ [1, 2, 3, 4, 24, 'c'], ['a', 2, 3, 'b', 24, 'd']];let resultArr = JSON.parse(JSON.stringify(arr));let tempArr = [];for (let i = 0; i < arr[0].length; i++) { for (let j = 0; j < arr.length; j++) { tempArr.push(arr[j][i]); } if (tempArr.every(item => item === tempArr[0])) { let index = resultArr[0].findIndex(p => p === tempArr[0]); resultArr = resultArr.map(val => { val.splice(index, 1); return val; }); } tempArr = [];}console.log(resultArr);所以,根据上面的逻辑,原来的答案会在下面const array1 = [ [{ checked: false, id: 13993, type: "checkbox", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "label", }, { checked: false, id: 13940, type: "label", }, ], [{ checked: false, id: 13993, type: "checkbox", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "checkbox", }, { checked: false, id: 13940, type: "label", }, ], [{ checked: false, id: 13993, type: "checkbox", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "checkbox", }, { checked: false, id: 13940, type: "label", }, ], [{ checked: false, id: 13993, type: "label", }, { checked: false, id: 13995, type: "label", }, { checked: false, id: 13998, type: "label", }, { checked: false, id: 13940, type: "label", }, ]];let resultArr = JSON.parse(JSON.stringify(array1));let tempArr = [];for (let i = 0; i < array1[0].length; i++) { for (let j = 0; j < array1.length; j++) { tempArr.push(array1[j][i]); } if (tempArr.every(item => item.type === tempArr[0].type && item.type === 'label')) { let index = resultArr[0].findIndex(p => p.id === tempArr[0].id); resultArr = resultArr.map(val => { val.splice(index, 1); return val; }); } tempArr = [];}console.log(resultArr);