【问题标题】:Select all objects in Array between DateTime range. How?在 DateTime 范围之间选择 Array 中的所有对象。如何?
【发布时间】:2021-09-04 05:18:51
【问题描述】:

我创建了这个例子来观察我可以用多少种方法来查询这个数组表,就像我做 MySQL 一样。

在下面的 8 个查询中,我无法弄清楚如何将 Array.find() 与 MySQL 日期时间范围 SELECT 一起使用。

更具体地说,查询 (6)。

const tblUsers = [{
    id: 101,
    user: 'user1',
    password: 'password1',
    created: '2021-01-01 00:00:01',
    modified: '2021-01-01 23:59:59',
    status: 'Active',
    firstName: 'Sean',
    lastName: 'Connery'
  },
  {
    id: 102,
    user: 'user2',
    password: 'password2',
    created: '2021-01-05 00:00:01',
    modified: '2021-01-05 23:59:59',
    status: 'Inactive',
    firstName: 'Bill',
    lastName: 'Murray'
  },
  {
    id: 103,
    user: 'user3',
    password: 'password3',
    created: '2021-01-10 00:00:01',
    modified: '2021-01-10 23:59:59',
    status: 'Active',
    firstName: 'Jeniffer',
    lastName: 'Connelly'
  },
];

let user4: any = {
  id: 104,
  user: 'user4',
  password: 'password4',
  created: '2021-01-15 00:00:01',
  modified: '2021-01-15 23:59:59',
  status: 'Active',
  firstName: 'Michelle',
  lastName: 'Pfeiffer'
};

// (0) - INSERT INTO tblUsers (id, user, password, ...) VALUES (104, 'user4', 'password4', ...);
tblUsers.push(user4);

// (1) - Select * FROM tblUsers;
let selectAlUsers: any = tblUsers || null;
console.log(tblUsers);

// (2) - Select * FROM tblUsers WHERE id = 103;
let selectUserById: any = tblUsers.find(record => record.id === 102) || null;
console.log(selectUserById);

// (3) - Select * FROM tblUsers WHERE firstName = 'Bill';
let selectUserByFirstName: any = tblUsers.find(record => record.firstName === 'Bill') || null;
console.log(selectUserByFirstName);

// (4) - Select * FROM tblUsers WHERE lastName = 'Murray';
let selectUserByLastName: any = tblUsers.find(record => record.lastName === 'Bill') || null;
console.log(selectUserByLastName);

// (5) - Select * FROM tblUsers WHERE status = 'Inactive';
let selectUsersByStatus: any = tblUsers.find(record => record.status === 'Inactive') || null;
console.log(selectUsersByStatus);

// (6) - Select * FROM tblUsers WHERE created BETWEEN '2021-01-05 00:00:01' AND '2021-01-10 00:00:01';
// let userRecordsByDateTimeRange: any = tblUsers.find(record => record.created === '2021-01-05 00:00:01' && record.created === '2021-01-10 23:59:59');
// console.log(userRecordsByDateTimeRange);

// (7) - UPDATE tblUsers SET modified = '2021-01-06 00:00:00', status = 'Active', ... WHERE id = 102;
function updateUserById(): boolean {

    let selectUserById: any = tblUsers.find(record => record.id === 102) || null;

    if(!selectUserById) return false;

    let user2: any = {
      // id: 102,
      // user: 'user2',
      // password: 'password2',
      // created: '2021-01-15 00:00:01',
      // modified: '2021-01-15 23:59:59',
      status: 'Active',
      // firstName: 'Bill',
      // lastName: 'Murray'
    };

    // Execute Update

    // selectUserById.user      = String(user2.user);
    // selectUserById.password  = String(user2.password);
    // selectUserById.created   = String(user2.created);
    // selectUserById.modified  = String(user2.modified);
    selectUserById.status    = String(user2.status);
    // selectUserById.firstName = String(user2.firstName);
    // selectUserById.lastName  = String(user2.lastName);
    return true;
}
console.log(updateUserById());

// (8) - DELETE FROM tblUsers WHERE id = 102;
function deleteUserById(): boolean {

    let selectUserById: any = tblUsers.find(record => record.id === 102) || null;
    let userArrayIndex: any = tblUsers.findIndex(record => record.id === 102) || null;

    if(!selectUserById) return false;
    if(!userArrayIndex) return false;

    // Execute Delete
    tblUsers.splice(userArrayIndex, 1); // remove 1 element at userArrayIndex

    return true;
}
console.log(deleteUserById());

【问题讨论】:

    标签: javascript arrays typescript deno


    【解决方案1】:

    您可以利用 new Date() 将时间戳转换为纪元,然后比较纪元。此外,您可以使用Array.filter() 返回多条记录。

    let userRecordsByDateTimeRange: any = tblUsers.filter(
        record => new Date(record.created).getTime() >= new Date('2021-01-05 00:00:01').getTime() 
        && new Date(record.created).getTime() <= new Date('2021-01-10 23:59:59').getTime()
    );
    console.log(userRecordsByDateTimeRange);
    

    【讨论】:

    • 由于Date 实现valueOf,您可以在不调用.getTime() 的情况下比较Date 实例。或者,可以使用 Date.parse 代替 Date 构造函数将日期字符串直接解析为纪元毫秒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多