js 数据结构:想剔除指定一列怎么操作?

[
 ['排序', '数量', '价值', '操作人', '时间','名称'],
 ['1', '22', '5.5', '小米', '2022-03','苹果'],
 ['2', '550', '3.5', '小王', '2022-03','香蕉'],
 ['3', '150', '13.5', '小李', '2022-03','西瓜']
]

如果我想剔除操作人和名称这一列怎么处理
想要的格式

[
 ['排序', '数量', '价值',  '时间'],
 ['1', '22', '5.5',  '2022-03'],
 ['2', '550', '3.5',  '2022-03'],
 ['3', '150', '13.5',  '2022-03']
]
阅读 5.1k
9 个回答
function deleteArrayWithIndex (arr, index) {
    const newArr = JSON.parse(JSON.stringify(arr))
    newArr.forEach(item => item.splice(index, 1))
    return newArr
}
a = [
 ['排序', '数量', '价值', '操作人', '时间','名称'],
 ['1', '22', '5.5', '小米', '2022-03','苹果'],
 ['2', '550', '3.5', '小王', '2022-03','香蕉'],
 ['3', '150', '13.5', '小李', '2022-03','西瓜']
]

deleteArrayWithIndex(deleteArrayWithIndex(a, 3), 4)

更新

function deleteArrayWithNames (arr, nameArr) {
    const nameIndexList = nameArr.map(item => arr[0].findIndex(name => name === item)) // 找到 name 的 index 数组
    const newArr = JSON.parse(JSON.stringify(arr))
    return newArr.map(item => item.filter((_item, index) => !nameIndexList.includes(index)))
}
a = [
 ['排序', '数量', '价值', '操作人', '时间','名称'],
 ['1', '22', '5.5', '小米', '2022-03','苹果'],
 ['2', '550', '3.5', '小王', '2022-03','香蕉'],
 ['3', '150', '13.5', '小李', '2022-03','西瓜']
]

deleteArrayWithNames(a, ['操作人', '名称'])
let list  = [
    ['排序', '数量', '价值', '操作人', '时间','名称'],
    ['1', '22', '5.5', '小米', '2022-03','苹果'],
    ['2', '550', '3.5', '小王', '2022-03','香蕉'],
    ['3', '150', '13.5', '小李', '2022-03','西瓜']
]
let indexArr = list[0].reduce((s, v, i) => ['操作人', '名称'].includes(v) ? s.push(i) && s : s, [])
let newList = list.map(v => v.filter((it, i) => !indexArr.includes(i)))
console.log(newList)
const arr = [
    ['排序', '数量', '价值', '操作人', '时间', '名称'],
    ['1', '22', '5.5', '小米', '2022-03', '苹果'],
    ['2', '550', '3.5', '小王', '2022-03', '香蕉'],
    ['3', '150', '13.5', '小李', '2022-03', '西瓜']
  ]
  const delColumns = []
  arr[0].forEach((item, index) => {
    if (['操作人', '名称'].includes(item)) {
      delColumns.push(index)
    }
  })
  const res = arr.map(item => {
    return item.filter((child, index) => !delColumns.includes(index))
  })
  console.log(res, 'res')

可以试试这个函数:

function removeColumns(arr, indexes) {
  indexes = indexes.map((idx) => arr[0].indexOf(idx));
  return arr.map((item) => {
    return item.filter((val, index) => {
      return !indexes.includes(index);
    });
  });
}

用法:

const data = [
  ["排序", "数量", "价值", "操作人", "时间", "名称"],
  ["1", "22", "5.5", "小米", "2022-03", "苹果"],
  ["2", "550", "3.5", "小王", "2022-03", "香蕉"],
  ["3", "150", "13.5", "小李", "2022-03", "西瓜"],
];
console.log(removeColumns(data, ["操作人", "名称"]));

输出:

[
  [ '排序', '数量', '价值', '时间' ],
  [ '1', '22', '5.5', '2022-03' ],
  [ '2', '550', '3.5', '2022-03' ],
  [ '3', '150', '13.5', '2022-03' ]
]

本方法,仅供参考

let list= [
    ['排序', '数量', '价值', '操作人', '时间','名称'],
    ['1', '22', '5.5', '小米', '2022-03','苹果'],
    ['2', '550', '3.5', '小王', '2022-03','香蕉'],
    ['3', '150', '13.5', '小李', '2022-03','西瓜']
]

let indexs = [];
list[0].forEach((item,index)=>{
    if(item == '操作人' || item === '名称'){
        indexs.push(index);
    }
})
let arr = list.reduce((prv,item,index)=>{
    item.forEach((i,j)=>{
        if(indexs.every(c=> c!=j)){
            prv[index] ? prv[index].push(i):prv[index] = [i]
        }
    })
    return prv
},[[]])

第一种

let arr = [
      ['排序', '数量', '价值', '操作人', '时间', '名称'],
      ['1', '22', '5.5', '小米', '2022-03', '苹果'],
      ['2', '550', '3.5', '小王', '2022-03', '香蕉'],
      ['3', '150', '13.5', '小李', '2022-03', '西瓜']
    ]

    function filterColumnArr(arr, sortIndexs = []) {
      return arr.map((item, i) => item.map((list, j) => {
        if (list === '操作人' || list === '时间') {
          sortIndexs.push(j)
        }

        if (!sortIndexs.includes(j)) {
          return list
        }

      }).filter(list => list !== undefined))
    }

    let res = filterColumnArr(arr)
    console.log(res);

第二种

let arr = [
      ['排序', '数量', '价值', '操作人', '时间', '名称'],
      ['1', '22', '5.5', '小米', '2022-03', '苹果'],
      ['2', '550', '3.5', '小王', '2022-03', '香蕉'],
      ['3', '150', '13.5', '小李', '2022-03', '西瓜']
    ]

    function filterColumnArr(arr, sortIndexs = []) {
      return arr.reduce((acc, curr, i) => {
        acc.push([])

        curr.forEach((list, j) => {
          if (list === '操作人' || list === '时间') {
            sortIndexs.push(j)
          }

          if (!sortIndexs.includes(j)) {
            acc[i].push(list)
          }

        })

        return acc
      }, [])
    }

    let res = filterColumnArr(arr)
    console.log(res);
const arr=[
 ['排序', '数量', '价值', '操作人', '时间','名称'],
 ['1', '22', '5.5', '小米', '2022-03','苹果'],
 ['2', '550', '3.5', '小王', '2022-03','香蕉'],
 ['3', '150', '13.5', '小李', '2022-03','西瓜']
]
const res = arr.map(item=>{
    return item.filter((_,index)=>index!==3&&index!==5)
})
function selectFields(data, selector) {
    const indexes = data[0]
        .map((field, i) => selector(field) ? i : -1)
        .filter(n => n >= 0);
    return data.map(line => indexes.map(i => line[i]));
}

// 调用示例
const result = selectFields(list, field => !["操作人", "名称"].includes(field));
console.log(result);

如果需要直接修改原数组

function selectFields(data, selector) {
    const removeIndexes = data[0]
        .map((field, i) => selector(field) ? -1 : i)
        .filter(n => n >= 0)
        .reverse();
    data.forEach(line => removeIndexes.forEach(i => line.splice(i, 1)));
    return data;
}

const result = selectFields(list, field => !["操作人", "名称"].includes(field));
console.log(result);

注意,第 2 个方法里。接口要求的 selector 还是需要保存的字段(保持接口涵义不变),内部处理是反向操作,找的要删除的 indexes,方便后面的 splice 操作。splice 操作按逆序(先删大 index)避免要删除的索引号产生变化。最后那个 return 要不要都可以,因为是直接修改的原数组,加 return 只是为了方便后续操作,比如 console.log(selectFields(...))

新手上路,请多包涵

function deleteArrayWithIndex (arr, oneindex, twoIndex ) {

  arr.forEach((item) => {
    item.splice(oneindex,1)
    item.splice(twoIndex,1)
  })

  return arr
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏