【问题标题】:Replace values in multiple rows in multiple columns provided a mapped object for each column替换多列中多行中的值,为每列提供一个映射对象
【发布时间】:2018-03-15 10:06:42
【问题描述】:

在下表中,我需要使用以下对象替换名称和类列中的值

+----------------+
| id name  class |
+----------------+
| 1   a     x    |
| 2   b     y    |
| 3   a     z    |
+----------------+

name 
{
    a: "John",
    b: "Jill"
}

class
{
    x: "Maths",
    y: "Science",
    z: "Arts"
}

最终表格应如下所示:

+----------------------+
|    id name  class    |
+----------------------+
| 1   John     Maths   |
| 2   Jill     Science |
| 3   John     Arts    |
+----------------------+

在 dexie.js 中实现这一目标的有效方法是什么?

【问题讨论】:

    标签: javascript dexie


    【解决方案1】:

    您需要扫描整个表格并逐行替换。

    var map = {
      "name": {
        "a": "John",
        "b": "Jill"
      },
      "class": {
        "x": "Maths",
        "y": "Science",
        "z": "Arts"
      }
    };
    
    var db = new Dexie('yourdbname');
    db.version(1).stores({
      yourTable: 'id'
    });
    
    db.yourTable.toCollection().modify(row => 
      Object.keys(map).forEach(column => row[column] = map[column][row[column]]));
    

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2019-05-07
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多