• JavaScript处理数组数据-数据匹配-剔除


    数据

    1. [
    2. {
    3. title: '市值',
    4. prop: 'sz',
    5. titleData: [
    6. {
    7. title: '市值',
    8. label: '市值',
    9. prop: 'sz',
    10. isShow: false,
    11. fixed: false,
    12. width: 100,
    13. align: 'left'
    14. },
    15. {
    16. title: '持仓/市值',
    17. label: '持仓/市值',
    18. prop: 'ccsz',
    19. isShow: false,
    20. fixed: false,
    21. width: 120,
    22. align: 'left'
    23. }
    24. ]
    25. },
    26. {
    27. title: '持仓',
    28. prop: 'cc',
    29. titleData: [
    30. {
    31. title: '资金费率',
    32. label: '资金费率',
    33. prop: 'avgFundingRateByOi',
    34. isShow: false,
    35. fixed: false,
    36. width: 100,
    37. align: 'left'
    38. },
    39. {
    40. title: '持仓',
    41. label: '持仓',
    42. prop: 'openInterest',
    43. isShow: false,
    44. fixed: false,
    45. width: 100,
    46. align: 'left'
    47. }
    48. ]
    49. }
    50. ]

    循环上面数组 并把titleData中的每一项和下面这个数组中每一项进行对比,单prop相等时,将下面的匹配项覆盖到上面对应的位置

    1. [{
    2. title: '持仓',
    3. label: '持仓',
    4. prop: 'openInterest',
    5. fixed: false,
    6. width: 100,
    7. isShow: true,
    8. align: 'left'
    9. },
    10. {
    11. title: '持仓变化(24h)',
    12. label: '持仓(24h%)',
    13. prop: 'h24OiChangePercent',
    14. fixed: false,
    15. width: 100,
    16. isShow: true,
    17. align: 'left'
    18. },
    19. {
    20. title: '多(4小时)',
    21. label: '多(4h)',
    22. prop: 'h4LongVolUsd',
    23. fixed: false,
    24. width: 100,
    25. isShow: true,
    26. align: 'left'
    27. }]

    实现

    1. data.forEach(item => {
    2. item.titleData.forEach(titleItem => {
    3. const match = newData.find(newItem => newItem.prop === titleItem.prop);
    4. if (match) {
    5. Object.assign(titleItem, match);
    6. }
    7. });
    8. });

    会遍历data数组中的每个对象,然后对每个对象的titleData数组进行遍历。在遍历titleData数组的过程中,会查找与newData数组中具有相同prop属性的对象。如果找到匹配项,则使用Object.assign方法将匹配项的属性覆盖到titleData数组中的相应对象上。

    最终,data数组中的titleData数组将被更新为匹配项的属性值。


    1. const data = [
    2. {
    3. label: "收藏",
    4. prop: "sc",
    5. fixed: true,
    6. width: 60,
    7. isShow: true,
    8. align: "center"
    9. },
    10. {
    11. label: "排名",
    12. prop: "pm",
    13. fixed: true,
    14. width: 60,
    15. isShow: true,
    16. align: "center"
    17. },
    18. {
    19. label: "币种",
    20. prop: "symbol",
    21. fixed: true,
    22. width: 90,
    23. isShow: true,
    24. align: "left"
    25. },
    26. {
    27. label: "价格",
    28. prop: "price",
    29. fixed: false,
    30. width: 100,
    31. isShow: true,
    32. align: "left"
    33. },
    34. {
    35. title: "价格变化(24h)",
    36. label: "价格(24h%)",
    37. prop: "h24PriceChangePercent",
    38. fixed: false,
    39. width: 100,
    40. isShow: true,
    41. align: "left"
    42. }
    43. ];

    循环上面数组 把下面的数字和上面匹配prop,当上面数组存在下面的某一项时,将其isshow字段赋值为下面的,如果isshow为false时,将从上面数组中删除,如果不存在则追加到上面数组中

    1. const newData = [
    2. {
    3. title: '持仓',
    4. label: '持仓',
    5. prop: 'openInterest',
    6. fixed: false,
    7. width: 100,
    8. isShow: true,
    9. align: 'left'
    10. },
    11. {
    12. title: '持仓变化(24h)',
    13. label: '持仓(24h%)',
    14. prop: 'h24OiChangePercent',
    15. fixed: false,
    16. width: 100,
    17. isShow: false,
    18. align: 'left'
    19. },
    20. {
    21. title: '多(4小时)',
    22. label: '多(4h)',
    23. prop: 'h4LongVolUsd',
    24. fixed: false,
    25. width: 100,
    26. isShow: true,
    27. align: 'left'
    28. }
    29. ];

    使用

    1. newData.forEach(newItem => {
    2. const matchIndex = data.findIndex(item => item.prop === newItem.prop);
    3. if (matchIndex !== -1) {
    4. if (newItem.isShow) {
    5. data[matchIndex].isShow = true;
    6. } else {
    7. data.splice(matchIndex, 1);
    8. }
    9. } else {
    10. data.push(newItem);
    11. }
    12. });
    13. console.log(data);
  • 相关阅读:
    学习笔记6——垃圾回收
    Spring注解驱动之@Autowired、@Qualifier、@Primary
    leetcode 20. 有效的括号
    C++进阶(二)
    Spring整合Junit单元测试
    华为机试 - 冠亚军排名
    掌握Mongodb,看完这篇文章就够了
    Ros noetic opencv4.2.0 cv_bridge 冲突 适配Opencv3.2.0完成自己的USB_Camera 跑通 Orb_slam2
    ubuntu18.04安装pangolin库,图文详解
    pytorch.反向传播算法和优化器
  • 原文地址:https://blog.csdn.net/JackieDYH/article/details/133925098