动态构建功能

时间:2020-07-06 07:15:17

标签: javascript arrays

第二次尝试解释问题,希望有人对我的问题有想法的解决方案。

我需要动态定义条件,以便可以在数组中找到index。 这是我的方法不起作用:

如果pks或以上有一条记录,则此_saveArray.findIndex(pkExist)无法正常工作。

  addToSaveArray(pks, row) {
    var condition = ''
    if(pks.length === 1) {
      condition = `a['${pks[0].pkField}'] === row['${pks[0].pkField}']`
    } else {
      pks.map(p => {
        const cond = `a['${p.pkField}'] === row['${p.pkField}'] && `
        condition = `${condition}${cond}`
      })
      condition = condition.slice(0, -3)
    }

    const pkExist = a => condition;
    const indForRemove = this._saveArray.findIndex(pkExist);

    if(indForRemove !== -1) {
      this._saveArray.splice(indForRemove, 1);
    }
    let arr = this._saveArray;
    arr.push(Object.assign({},row));
  }

此代码有效,但仅适用于一个字段

const pkExist = a => a[pks[0].pkField] === row[pks[0].pkField]

问题是如何为索引动态创建查询记录条件。

预先感谢

0 个答案:

没有答案
相关问题