第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從對象值的多維數(shù)組中動態(tài)刪除列

從對象值的多維數(shù)組中動態(tài)刪除列

哆啦的時光機 2023-01-06 11:30:12
我正在嘗試從多維對象數(shù)組中刪除該列,下面是多維對象數(shù)組和我嘗試過的代碼:-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);在上面的數(shù)組中,我想刪除第二列和第四列,因為它們的值相同,我發(fā)現(xiàn)了上面的邏輯,但在這里我想動態(tài)傳遞索引而不是 2,我已經(jīng)嘗試了很多組合它起作用,我也嘗試傳遞 array1 索引但不刪除正確的列。如果我動態(tài)傳遞索引,那么在數(shù)組中,如果該列重復(fù),它將自動刪除。
查看完整描述

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


查看完整回答
反對 回復(fù) 2023-01-06
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗 獲得超21個贊

不是一個完美的解決方案,但得到預(yù)期的結(jié)果:

  1. id's兩個過濾器13993,13998

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


查看完整回答
反對 回復(fù) 2023-01-06
?
手掌心

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)


查看完整回答
反對 回復(fù) 2023-01-06
?
搖曳的薔薇

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


查看完整回答
反對 回復(fù) 2023-01-06
  • 4 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號