const fs = require("fs")
const data = JSON.parse(fs.readFileSync("test-data.json"))
function isEqual(o, name, targets) {
return targets.some((t) => {
return t === o[name]
})
}
function isContainList(o, name, targets) {
return targets.some((t) => {
return o[name].includes(t)
})
}
function addIndex(indexHash, index) {
if (indexHash[index]) {
indexHash[index]++
} else {
indexHash[index] = 1
}
}
let conditions = JSON.parse(fs.readFileSync("condition.json")).items
let checkCount = conditions.length
let indexHash = {}
data.items.map((x, index) => {
for (let i = 0; i < conditions.length; i++) {
if (conditions[i].type === "single") {
if (isEqual(x, conditions[i].property, conditions[i].targets)) {
addIndex(indexHash, index)
}
} else {
if (isContainList(x, conditions[i].property, conditions[i].targets)) {
addIndex(indexHash, index)
}
}
}
})
let uniqIndexList = []
Object.keys(indexHash).forEach((x) => {
if (indexHash[x] === checkCount) {
uniqIndexList.push(x)
}
})
console.log(uniqIndexList)
// condition.json
{
"items": [
{ "property": "name", "targets": ["AA", "CC"], "type": "single" },
{ "property": "old", "targets": [100, 103], "type": "single" },
{ "property": "nums", "targets": [2, 6], "type": "list" }
]
}
// test-data.json
{
"items": [
{ "name": "AA", "old": 100, "nums": [1, 2, 3] },
{ "name": "AA", "old": 102, "nums": [4, 5, 6] },
{ "name": "BB", "old": 103, "nums": [7, 8, 9] },
{ "name": "CC", "old": 104, "nums": [1, 2, 3] },
{ "name": "BB", "old": 105, "nums": [4, 5, 6] },
{ "name": "DD", "old": 100, "nums": [7, 8, 9] },
{ "name": "EE", "old": 102, "nums": [1, 2, 3] },
{ "name": "FF", "old": 103, "nums": [4, 5, 6] },
{ "name": "FF", "old": 104, "nums": [7, 8, 9] },
{ "name": "AA", "old": 105, "nums": [1, 2, 3] },
{ "name": "AA", "old": 100, "nums": [4, 5, 6] },
{ "name": "FF", "old": 101, "nums": [7, 8, 9] },
{ "name": "CC", "old": 103, "nums": [1, 2, 3] },
{ "name": "BB", "old": 104, "nums": [4, 5, 6] }
]
}