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

我正在尝试从多维对象数组中删除该列,下面是多维对象数组和我尝试过的代码:-


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 索引但不删除正确的列。


如果我动态传递索引,那么在数组中,如果该列重复,它将自动删除。


哆啦的时光机
浏览 137回答 4
4回答

隔江千里

&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; { id: 1 },&nbsp; &nbsp; &nbsp; &nbsp; { id: 2 },&nbsp; &nbsp; &nbsp; &nbsp; { id: 3 },&nbsp; &nbsp; &nbsp; &nbsp; { id: 4 },&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; { id: 2 },&nbsp; &nbsp; &nbsp; &nbsp; { id: 2 },&nbsp; &nbsp; &nbsp; &nbsp; { id: 3 },&nbsp; &nbsp; &nbsp; &nbsp; { id: 5 },&nbsp; &nbsp; ],]function createNewArray(arr, n) {&nbsp; &nbsp; const newArr = [];&nbsp; &nbsp; arr.forEach(element => {&nbsp; &nbsp; &nbsp; &nbsp; newArr.push(element[n])&nbsp; &nbsp; });&nbsp; &nbsp; return newArr;}function getResult() {&nbsp; &nbsp; const tempArray = [];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (let index = 0; index < origin[0].length; index++){&nbsp; &nbsp; &nbsp; &nbsp; const newArray = createNewArray(origin, index).map(e => e.id);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ([...new Set(newArray)].length > 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempArray.push(newArray);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let result = Array(tempArray[0].length).fill([]);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return result.map((item, index) => tempArray.map(e => e[index]));}console.log('result', getResult());

茅侃侃

不是一个完美的解决方案,但得到预期的结果:id's两个过滤器13993,13998检查id并相应地修改值const array1 = [&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ],&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ],&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ],&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ]];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) => {&nbsp; table.forEach(row => row.splice(colIndex, 1))&nbsp;&nbsp;}removeCol(array1, 0); // To remove column 0 (0-indexed)removeCol(array1, 1); // To remove column 1 (0-indexed)

摇曳的蔷薇

也许你可以像下面这样尝试let arr = [&nbsp; [1, 2, 3, 4, 24, 'c'],&nbsp; ['a', 2, 3, 'b', 24, 'd']];let resultArr = JSON.parse(JSON.stringify(arr));let tempArr = [];for (let i = 0; i < arr[0].length; i++) {&nbsp; for (let j = 0; j < arr.length; j++) {&nbsp; &nbsp; tempArr.push(arr[j][i]);&nbsp; }&nbsp; if (tempArr.every(item => item === tempArr[0])) {&nbsp; &nbsp; let index = resultArr[0].findIndex(p => p === tempArr[0]);&nbsp; &nbsp; resultArr = resultArr.map(val => {&nbsp; &nbsp; &nbsp; val.splice(index, 1);&nbsp; &nbsp; &nbsp; return val;&nbsp; &nbsp; });&nbsp; }&nbsp; tempArr = [];}console.log(resultArr);所以,根据上面的逻辑,原来的答案会在下面const array1 = [&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ],&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ],&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "checkbox",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ],&nbsp; [{&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13993,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13995,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13998,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; checked: false,&nbsp; &nbsp; &nbsp; id: 13940,&nbsp; &nbsp; &nbsp; type: "label",&nbsp; &nbsp; },&nbsp; ]];let resultArr = JSON.parse(JSON.stringify(array1));let tempArr = [];for (let i = 0; i < array1[0].length; i++) {&nbsp; for (let j = 0; j < array1.length; j++) {&nbsp; &nbsp; tempArr.push(array1[j][i]);&nbsp; }&nbsp; if (tempArr.every(item => item.type === tempArr[0].type && item.type === 'label')) {&nbsp; &nbsp; let index = resultArr[0].findIndex(p => p.id === tempArr[0].id);&nbsp; &nbsp; resultArr = resultArr.map(val => {&nbsp; &nbsp; &nbsp; val.splice(index, 1);&nbsp; &nbsp; &nbsp; return val;&nbsp; &nbsp; });&nbsp; }&nbsp; tempArr = [];}console.log(resultArr);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript