js:数组对象,如何把上面的数据,处理成下面的数据

[{carNumber: "XXXXXXXX"
checkBy: "cpb"
checkBy_dictText: "小明"
checkOpinion: "测试数据"
checkStatus: "03"
checkStatus_dictText: "已拒绝"
checkTime: "2022-04-01 17:51:53"
companyName: "公司名称"
createBy: null
createTime: "2022-04-01 17:50:59"
enableCar: "01"
enableCar_dictText: "是"
id: "1509830673821495298"
mobile: "18811111111"
name: "李四"
releaseGoods: "电脑"
releaseTime: "2022-04-01"
updateBy: "cpb"
updateTime: "2022-04-01 17:51:53"
userAuthId: "1509099044442374146"
},
{carNumber: "XXXXXXXX"
checkBy: "cpb"
checkBy_dictText: "小红"
checkOpinion: "测试数据"
checkStatus: "03"
checkStatus_dictText: "已拒绝"
checkTime: "2022-04-01 17:51:53"
companyName: "公司名称"
createBy: null
createTime: "2022-04-01 17:50:59"
enableCar: "01"
enableCar_dictText: "是"
id: "1509830673821495298"
mobile: "18811111111"
name: "张三"
releaseGoods: "笔记本"
releaseTime: "2022-04-01"
updateBy: "cpb"
updateTime: "2022-04-01 17:51:53"
userAuthId: "1509099044442374146"
}]
 [{
        checkStatus_dictText: '02',
        createTime: '2022.03.11',
        releaseTime: '2022.03.22',
        releaseGoods: '电脑',
        carNumber: 'XXXXXX'
      },
 {
        checkStatus_dictText: '02',
        createTime: '2022.03.11',
        releaseTime: '2022.03.22',
        releaseGoods: '电脑',
        carNumber: 'XXXXXX'
      },
],
阅读 1.8k
4 个回答
const data = [
  {
    carNumber: "XXXXXXXX",
    checkBy: "cpb",
    checkBy_dictText: "小明",
    checkOpinion: "测试数据",
    checkStatus: "03",
    checkStatus_dictText: "已拒绝",
    checkTime: "2022-04-01 17:51:53",
    companyName: "公司名称",
    createBy: null,
    createTime: "2022-04-01 17:50:59",
    enableCar: "01",
    enableCar_dictText: "是",
    id: "1509830673821495298",
    mobile: "18811111111",
    name: "李四",
    releaseGoods: "电脑",
    releaseTime: "2022-04-01",
    updateBy: "cpb",
    updateTime: "2022-04-01 17:51:53",
    userAuthId: "1509099044442374146"
  },
  {
    carNumber: "XXXXXXXX",
    checkBy: "cpb",
    checkBy_dictText: "小红",
    checkOpinion: "测试数据",
    checkStatus: "03",
    checkStatus_dictText: "已拒绝",
    checkTime: "2022-04-01 17:51:53",
    companyName: "公司名称",
    createBy: null,
    createTime: "2022-04-01 17:50:59",
    enableCar: "01",
    enableCar_dictText: "是",
    id: "1509830673821495298",
    mobile: "18811111111",
    name: "张三",
    releaseGoods: "笔记本",
    releaseTime: "2022-04-01",
    updateBy: "cpb",
    updateTime: "2022-04-01 17:51:53",
    userAuthId: "1509099044442374146"
  }
];

const result = data.map((item) => ({
  checkStatus_dictText: item.checkStatus,
  createTime: item.createTime,
  releaseTime: item.releaseTime,
  releaseGoods: item.releaseGoods,
  carNumber: item.carNumber
}));

console.log(result);

image.png

先没注意到还有数据转换,所以在原来的基础上修改了一下答案。

假设原数据在 data 中。然后为了日期数据的转换,准备了一个简单的转换函数,就是利用正则表达式来处理了下

function formatDate(s) {
    return s.replace(/(\d+)-(\d+)-(\d+).*/, "$1.$2.$3");
}

先上一个手写的写法,利用参数解构

const result = data.map(
    ({
        checkStatus: checkStatus_dictText,
        createTime,
        releaseTime,
        releaseGoods,
        carNumber,
    }) => ({
        checkStatus_dictText,
        createTime: formatDate(createTime),
        releaseTime: formatDate(releaseTime),
        releaseGoods,
        carNumber,
    })
);

这里 checkStatus: checkStatus_dictText 是把 checkStatus 属性取出来,但赋给了一个叫 checkStatus_dictText 的临时变量,解构以及后面的构造直接对象,就像是在新对象里把属性名改了一样。

再来一个属性列表的写法,先把需要的属性存在一个列表中(attrs),然后把每个对象拆成 entries,找到需要的,重新组合成对象 —— 如果只是选择属性可以这样写。

但是现在不仅要选择属性,还要做一定的转换,所以不能用列表,可用映射表,表明转换关系。

const attrs = {
    "checkStatus_dictText": "checkStatus",
    "createTime": formatDate,
    "releaseTime": formatDate,
    "releaseGoods": true,
    "carNumber": true,
};

这个映射表配置的转换关系,是准备使用这样的逻辑:

  • 如果是 true,直接用原属性值;空值(nullundefined 等)直接忽略掉,即不要这个属性。
  • 如果配置的是字符串,表示使用对应的属性,比如 "checkStatus_dictText": "checkStatus"
  • 如果配置的函数,表示要用这个函数对原值进行处理,并使用其结果

这个转换过程用一个函数表示大概是这样:

function getAttrValue(it, key) {
    const config = attr[key];
    switch (typeof config) {
        case "undefiend":
            return undefined;
        case "string":
            return it[config];
        case "function":
            return config(it[key]);
        default:
            return it[key];
    }
}

很繁琐,所以我们不妨把配置改改,直接改成处理函数,输入参数是对象本身,那么

// 注意,下面都是用的这个配置
const attrs = {
    "checkStatus_dictText": it => it.checkStatus,
    "createTime": it => formatDate(it.createTime),
    "releaseTime": it => formatDate(it.releaseTime),
    "releaseGoods": it => it.releaseGoods,
    "carNumber": it => it.carNumber,
};

使用的时候也方便,只需要

const getAttrValue = (it, key) => attr[key]?.(it);

然后接下来继续,使用上面的 attrs 配置来拆解/组合

const result = data.map(it => {
    return Object.fromEntries(
        Object.entries(it)
            .filter(([key]) => key in attrs)
            .map(([key]) => [key, attrs[key]?.(it)])
    );
});

不过与其在 entries 中去过滤,不如直接用 attrs 的 key 集合。这里要先从 attrs 拿到其 entries 列表,而且每次循环都要用,所以干脆放在外面用一个临时变量保存起来:

const attrEntries = Object.entries(attrs);
const result = data.map(it => {
    return Object.fromEntries(
        attrEntries.map(([key, fn]) => [key, fn(it)])
    );
});

或者都不用 Object.fromEntries,直接用 reduce 拼对象

const attrEntries = Object.entries(attrs);
const result = data.map(it => attrEntries.reduce(
    (r, [key, fn]) => (r[key] = fn(it), r),
    {}
));

然而这种情况下就没办法用 Lodash 的 pick() 函数了。不过下面的例子留下来,演示 pick 函数用法

const attrs = [
    "checkStatus_dictText",
    "createTime",
    "releaseTime",
    "releaseGoods",
    "carNumber",
];
const result = data.map(it => _.pick(it, attrs));

循环判断下,把想要留下来的字段存起来就可以。

const filter = (arr) => {
  return arr.map((x) => {
    const o = {};
    const checkStatusMap = {
      已通过: "01",
      已拒绝: "02",
    };

    o.checkStatus_dictText = checkStatusMap[x.checkStatus_dictText];
    o.createTime = x.createTime.substring(0, 10).replace(/-/g, ".");
    o.releaseTime = x.releaseTime.substring(0, 10).replace(/-/g, ".");
    o.releaseGoods = x.releaseGoods;
    o.carNumber = x.carNumber;
    return o;
  });
};

简单写了下,抛砖引玉

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