为了账号安全,请及时绑定邮箱和手机立即绑定

从对象值的多维数组中动态删除列

从对象值的多维数组中动态删除列

哆啦的时光机 2023-01-06 11:30:12
我正在尝试从多维对象数组中删除该列,下面是多维对象数组和我尝试过的代码:-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",    },  ]];array1.map((a, i) => {  a.splice(2, 1);  console.log(a)  return a;});console.log(array1);在上面的数组中,我想删除第二列和第四列,因为它们的值相同,我发现了上面的逻辑,但在这里我想动态传递索引而不是 2,我已经尝试了很多组合它起作用,我也尝试传递 array1 索引但不删除正确的列。如果我动态传递索引,那么在数组中,如果该列重复,它将自动删除。
查看完整描述

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());


查看完整回答
反对 回复 2023-01-06
?
茅侃侃

TA贡献1842条经验 获得超21个赞

不是一个完美的解决方案,但得到预期的结果:

  1. id's两个过滤器1399313998

  2. 检查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)


查看完整回答
反对 回复 2023-01-06
?
手掌心

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)


查看完整回答
反对 回复 2023-01-06
?
摇曳的蔷薇

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);


查看完整回答
反对 回复 2023-01-06
  • 4 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信