我有一根弦,就像,
const string =
"DEVICE_SIZE IN ('036','048','060','070') AND DEVICE_VOLTAGE IN ('1','3') AND NOT DEVICE_DISCHARGE_AIR IN ('S') AND NOT DEVICE_REFRIGERANT_CIRCUIT IN ('H','C')";
从这里,我需要映射各自的键和值,如DEVICE_SIZE: ["036", "048", "060", "070"]
Current Result:个
const string =
"DEVICE_SIZE IN ('036','048','060','070') AND DEVICE_VOLTAGE IN ('1','3') AND NOT DEVICE_DISCHARGE_AIR IN ('S') AND NOT DEVICE_REFRIGERANT_CIRCUIT IN ('H','C')";
const res = string.split('IN ');
const regExp = /\(([^)]+)\)/;
const Y = 'AND';
const data = res.map((item) => {
if (regExp.exec(item)) {
return {
[item.slice(item.indexOf(Y) + Y.length)]: regExp.exec(item)[1],
};
}
});
console.log('data ', data);
Expected Result:个
[
{ "DEVICE_SIZE": ["036", "048", "060", "070"] },
{ "DEVICE_VOLTAGE": ["1", "3"] },
{ "NOT DEVICE_DISCHARGE_AIR": ["s"] },
{ "NOT DEVICE_REFRIGERANT_CIRCUIT": ["H", "C"] },
];
根据我在当前结果中的try ,我无法获得确切的结果.您能帮我达到上述预期效果吗?
Note.我正在try 上述做法,以达到我上一个问题中提到的最终结果 How to get valid object from the string matching respective array?个