4 回答
TA贡献1906条经验 获得超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贡献1842条经验 获得超21个赞
不是一个完美的解决方案,但得到预期的结果:
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 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贡献1942条经验 获得超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贡献1793条经验 获得超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);
所以,根据上面的逻辑,原来的答案会在下面
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);
添加回答
举报