4 回答

TA貢獻(xiàn)1906條經(jīng)驗 獲得超10個贊
[
{ 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());

TA貢獻(xiàn)1842條經(jīng)驗 獲得超21個贊
不是一個完美的解決方案,但得到預(yù)期的結(jié)果:
id's
兩個過濾器13993
,13998
檢查
id
并相應(yīng)地修改值
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 step
let 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 way
console.log(result)

TA貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊
你的意思是這樣的嗎?
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)

TA貢獻(xiàn)1793條經(jīng)驗 獲得超6個贊
也許你可以像下面這樣嘗試
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);
所以,根據(jù)上面的邏輯,原來的答案會在下面
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);
添加回答
舉報