I have this array of objects
var test = [{
jobId: 1,
requiredNumber: 1,
actualReq: 1,
},{
jobId: 2,
requiredNumber: 1,
actualReq: 1,
},{
jobId: 3,
requiredNumber: 1,
actualReq: 1,
},{
jobId: 4,
requiredNumber: 1,
actualReq: 1,
}]
and this array of objects
var testArr = [{ jobId: 1, current: 1 },
{ jobId: 2, current: 1 },
{ jobId: 4, current: 1 }]
I want to compare these arrays and return a flag if jobId is missing from the 2nd array like jobId 3
or if the current
of jobId
is smaller than actualReq
from the 1st array
what I have done is making nested loops for each array and compare but it didn't work for me
var y = false;
for (let i = 0; i < test.length; i ) {
for (let j = 0; j < testArr.length; j ) {
if (test[i].jobId === testArr[j].jobId) {
if (test[i].actualReq <= testArr[j].current) {
y = false
} else {
y =true
}
} else {
y = true;
}
}
}
console.log(y);
it always returns true
CodePudding user response:
You have to break from the loop when you find the one which is true
var test = [{
jobId: 1,
requiredNumber: 1,
actualReq: 1,
}, {
jobId: 2,
requiredNumber: 1,
actualReq: 1,
}, {
jobId: 3,
requiredNumber: 1,
actualReq: 1,
}, {
jobId: 4,
requiredNumber: 1,
actualReq: 1,
}]
var testArr = [{
jobId: 1,
current: 1
},
{
jobId: 2,
current: 1
},
{
jobId: 4,
current: 1
}
]
var y = false;
main:
for (let i = 0; i < test.length; i ) {
for (let j = 0; j < testArr.length; j ) {
if (test[i].jobId === testArr[j].jobId) {
if (test[i].actualReq <= testArr[j].current) {
y = false
} else {
y = true
break main
}
} else {
y = true;
break
}
}
}
console.log(y);
I would use every
or some
instead
const test = [{
jobId: 1,
requiredNumber: 1,
actualReq: 1,
}, {
jobId: 2,
requiredNumber: 1,
actualReq: 1,
}, {
jobId: 3,
requiredNumber: 1,
actualReq: 1,
}, {
jobId: 4,
requiredNumber: 1,
actualReq: 1,
}]
const testArr = [{
jobId: 1,
current: 1
},
{
jobId: 2,
current: 1
},
{
jobId: 4,
current: 1
}
]
const result = test.every(job => {
const other = testArr.find(j => job.jobId === j.jobId)
if (!other) return false
return job.actualReq <= other.current
})
console.log(result)
CodePudding user response:
One-liner solution to your problem statement using. I have not updated the logic of yours
test.forEach(value=> console.log(testArr.some(item => value.jobId === item.jobId && value.actualReq <= item.current)) )
if you want the list of objects with true/false values you can use the filter
method
true
:
test.filter(value=> testArr.some(item => value.jobId === item.jobId && value.actualReq <= item.current) )
false
:
test.filter(value=> !testArr.some(item => value.jobId === item.jobId && value.actualReq <= item.current) )
CodePudding user response:
You should be able to do this with one loop, looping over the first array. You can then use Array.Find to see if the job ID exists in the second array. If not, you can return (or do whatever you want) and then compare the second objective, the actualReq
vs the current
.
var test = [{
jobId: 1,
requiredNumber: 1,
actualReq: 1,
},{
jobId: 2,
requiredNumber: 1,
actualReq: 1,
},{
jobId: 3,
requiredNumber: 1,
actualReq: 1,
},{
jobId: 4,
requiredNumber: 1,
actualReq: 2,
}]
var testArr = [{ jobId: 1, current: 1 },
{ jobId: 2, current: 1 },
{ jobId: 4, current: 1 }]
test.forEach((t) => {
const matching = testArr.find((ta) => ta.jobId === t.jobId)
// The jobId is missing, log it and return it
if (!matching) {
console.log(t.jobId, 'is missing')
return t.jobId
}
if (matching.current < t.actualReq) {
console.log(`Job ID: ${matching.jobId} -`, matching.current, 'is less than', t.actualReq)
}
})