在nodejs中执行以下操作的更好方法:比较导入的csv或对象数组的行中是否存在特定的键值对



问题:

我想取一个有4个键值对的对象,并将其与一个也包含这4个键的对象数组进行比较。我想检查对象数组中4个键的值是否与引用对象中的值匹配。

当前实施

下面的代码起作用,作为测试,它会生成一个数组,检查"code"键值是否与引用对象"testCheck"的键值匹配。应该扩展代码以检查年份和月份是否为当前年份和月份以及引用对象的id(我知道如何从节点中获取年份和月份(。

我想知道的是,有更好的方法可以做到这一点,也许是更适合规模的,或者只是写得更好的代码

toDB->在生产中,这将来自数据库,管理员可以通过管理门户动态管理数据库。

testarr->这只是一个片段,但在生产中,它将来自上传的csv。

testCheck->在生产中,此对象将根据DB中的数据创建,该数据应再次由管理员管理。

代码

let testarr = [
{
Account: "1110-000",
Description: "Cash - Operating",
Amount: "-2,733,864.88",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
{
Account: "1116-000",
Description: "Cash in Trust",
Amount: "35,431.85",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
{
Account: "1170-000",
Description: "Petty Cash",
Amount: "0",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
];
let toDB = { code: "cat", id: "cat12345" };
let testCheck = {
Code: toDB["code"],
Year: "2020",
Month: "1",
Id: toDB["id"],
};
confirmTheSame = (data) => {
return data.map((x) => {
if (x["Code"] === testCheck["Code"]) {
return true;
} else {
return false;
}
});
};
let result = confirmTheSame(testarr);
console.log(result);

编辑1

发现这在将testCheck与对象数组映射时效果更好,并为每行生成布尔数组(数组数组(,但可能不是最好的方法?把这个留给你思考。。

confirmTheSame = (data) => {
return data.map((x) => {
return Object.keys(testCheck).map((y) => {
if(x[y]===testCheck[y]){
return true
}
else{
return false
}
});
});
};

对此有不同的方法。像lodash这样的Util也可以帮助代码变得更干净。

在不使用lodash的情况下,您可以在数组上使用findIndex函数来判断对象是否存在,如果索引大于-1 ,则返回true

const testarr = [
{
Account: "1110-000",
Description: "Cash - Operating",
Amount: "-2,733,864.88",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
{
Account: "1116-000",
Description: "Cash in Trust",
Amount: "35,431.85",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
{
Account: "1170-000",
Description: "Petty Cash",
Amount: "0",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
];
const toDB = {
Code: "cat",
ID: "cat12345"
};
const testCheck = {
Code: toDB.Code,
Year: "2020",
Month: "1",
ID: toDB.ID,
};
const confirmTheSame = (arr, obj) => {
const today = new Date()
const currentYear = today.getFullYear().toString()
const currentMonth = today.getMonth().toString()
const index = arr.findIndex(x => x.Year === currentYear && x.Month === currentMonth && x.Code === obj.Code && x.ID === obj.ID)
return index > -1 ? true : false
};
const result = confirmTheSame(testarr, testCheck)
console.log(result);

听起来你认为你只会比较一组特定的4个键,但这里有一个通用的答案,应该允许你比较testCheck:中任意数量的键

function confirmTheSame(obj) {
// `Id` and `ID` keys are different in the 'test' and 'check' objects,
// so normalize them here. You could skip this if you know the keys will 
// always match.
const newObj = Object.keys(obj).reduce((acc, key) => ({
...acc,
[key.toLowerCase()]: obj[key],
}), {});
// Use `Array.every` to check that all the values for the given object match.
return Object.keys(testCheck).every((key) => (
// Normalize the key here as well.
newObj[key.toLowerCase()] === testCheck[key]
));
}

全样本:

let testarr = [
{
Account: "1110-000",
Description: "Cash - Operating",
Amount: "-2,733,864.88",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
{
Account: "1116-000",
Description: "Cash in Trust",
Amount: "35,431.85",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
{
Account: "1170-000",
Description: "Petty Cash",
Amount: "0",
Code: "cat",
Year: "2020",
Month: "1",
ID: "cat12345",
},
];
let toDB = { code: "cat", id: "cat12345" };
let testCheck = {
Code: toDB["code"],
Year: "2020",
Month: "1",
Id: toDB["id"],
};
function confirmTheSame(obj) {
const newObj = Object.keys(obj).reduce((acc, key) => ({
...acc,
[key.toLowerCase()]: obj[key],
}), {});
return Object.keys(testCheck).every((key) => (
newObj[key.toLowerCase()] === testCheck[key]
));
}
console.log(testarr.map(confirmTheSame));

最新更新