javascript 对象数组动态连接和过滤器

koaltpgm  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(150)

我正在处理一个相当复杂的情况:我有3个数组,如下所示。Values数组中的值每隔5秒就会更改为truefalse

var Config = [{
    "rdd": "Transducer Failure",
    "performance": true,
    "agc": false,
    "snr": true,
    "sos": false,
    "flowvel": true
  },
  {
    "rdd": "Detection Problem",
    "performance": false,
    "agc": true,
    "snr": false,
    "sos": true,
    "flowvel": false
  }
]

配置数组将包含所有示例的配置,所有示例均适用于相应的rdd。例如,"传感器故障"将包含performancesnrflowvel,而不是所有其他字段;ds将适用于agcsos

var Instance = [{
  "U": [{
    "performance": "abc",
    "agc": "xyz",
    "snr": "pqr",
    "sos": "vns",
    "flowvel": "mns"
  }],
  "Y": [{
    "performance": "a",
    "agc": "b",
    "snr": "c",
    "sos": "d",
    "flowvel": "e"
  }]
}]

示例将具有配置数组为的示例:

var Values = [{
  "abc": true,
  "xyz": false,
  "pqr": true,
  "vns": false,
  "mns": true,
  "a": true,
  "b": false,
  "c": false,
  "d": true,
  "e": true
}]

值将具有相对于示例值的值。
我想要新的对象数组匹配以下条件..
1.如果Config中的值是true,例如,如果配置数组中的performancesnr为真,则它Map到Instance数组中的相应值,例如,如果performancetrue,则它将Map到Instance数组中的"abc",并且如果performance为假,则它将返回NA

  1. Config数组中键的值将是Values数组中键的值。例如,如果performancetrue,则将其Map到示例"abc",如果"abc"的值是true,则performance的值将是truefalse。其次,如果Config本身中performance值为false,则返回"NA"
    期望产量
result = [{
  "U": [{
      "rdd": "Transducer Failure",
      "performance": true,
      "agc": "NA",
      "snr": true,
      "sos": "NA",
      "flowvel": true
    },
    {
      "rdd": "Detection Problem",
      "performance": "NA",
      "agc": false,
      "snr": "NA",
      "sos": false,
      "flowvel": "NA"
    }
  ],
  "Y": [{
      "rdd": "Transducer Failure",
      "performance": true,
      "agc": "NA",
      "snr": false,
      "sos": "NA",
      "flowvel": true
    },
    {
      "rdd": "Detection Problem",
      "performance": "NA",
      "agc": false,
      "snr": "NA",
      "sos": true,
      "flowvel": "NA"
    }
  ]
}]
k10s72fa

k10s72fa1#

下面是我的解决方案。我使用Object.keys来获取键名称,并使用数组表示法在结果对象键中分配动态名称。Object.assign用于在最终结果中仅获取一个对象,即resultArray

var Config = [
    {
        "rdd": "Transducer Failure",
        "performance": true,
        "agc": false,
        "snr": true,
        "sos": false,
        "flowvel": true
    },
    {
        "rdd": "Detection Problem",
        "performance": false,
        "agc": true,
        "snr": false,
        "sos": true,
        "flowvel": false
    }
]

var Instance = [{
    "U": [{
        "performance": "abc",
        "agc": "xyz",
        "snr": "pqr",
        "sos": "vns",
        "flowvel": "mns"
    }],
    "Y": [{
        "performance": "a",
        "agc": "b",
        "snr": "c",
        "sos": "d",
        "flowvel": "e"
    }]
}]

var Values = [{
    "abc": true,
    "xyz": false,
    "pqr": true,
    "vns": false,
    "mns": true,
    "a": true,
    "b": false,
    "c": false,
    "d": true,
    "e": true
}]

function getResult(Instance, config, v) {
    let resultArray = [];
    for (let i = 0; i < Instance.length; i++) {
        const resultKeys = Object.keys(Instance[i])
        const element = Instance[i];
        let prev = null
        resultKeys.forEach((r, index) => {
            let reuslt = {
                [r]: config.map(c => {
                    const configKeys = Object.keys(c)
                    return {
                        [configKeys[0]]: c.rdd,
                        [configKeys[1]]: c[configKeys[1]] ? v[0][element[r][i][configKeys[1]]] : 'NA',
                        [configKeys[2]]: c[configKeys[2]] ? v[0][element[r][i][configKeys[2]]] : 'NA',
                        [configKeys[3]]: c[configKeys[3]] ? v[0][element[r][i][configKeys[3]]] : 'NA',
                        [configKeys[4]]: c[configKeys[4]] ? v[0][element[r][i][configKeys[4]]] : 'NA',
                        [configKeys[5]]: c[configKeys[5]] ? v[0][element[r][i][configKeys[5]]] : 'NA'
                    }
                })
            }
            if (index === 0) {
                prev = reuslt
            }
            else {
                Object.assign(prev, reuslt)
            }
        })
        resultArray.push(prev)
    }
    console.log(resultArray)
}
getResult(Instance, Config, Values)

相关问题