This is my data output.
var results = [ [ '(not set)', '87793', '6000' ],
[ '(other)', '1146', '28000' ],
[ 'test', '22', '7000' ]
]
You will see above that the first index element value is set to not set
but in some case, the output does not have a not set
value such as this:
var results = [ [ 'key', '87793', '6000' ],
[ '(other)', '1146', '28000' ],
[ 'test', '22', '7000' ]
]
I want to output the value, that if not set is in the results output, console. log('Not Set Data is there)
and if not set is not available then console.log ('Not Set Data is not found');
My Attempt to produce the above logic
var results = [
['(not set)', '87793', '1261928.2330840048'],
['(other)', '1146', '28096.821814000003'],
['test', '22', '215.958067']
]
var final = {};
for (var i = 0; i < results.length; i++) {
console.log(results[i][0]);
if (results[i][0] === '(not set)' || results[i][0] === '') {
final.status = 'Not Set is found';
} else {
final.status = 'Not Set is NOT found';
}
}
console.log(final)
The issue is when I do console.log, I see the output below, shouldn’t I see 'Not Set Data is there'
:
{
status: "Not Set is NOT found"
}
3
Answers
You can use a boolean flag to check if "(not set)" is found in any of the arrays. If found, set the flag to
true
. After the loop, check the flag to determine whether "(not set)" is present or not.You can use
some
You could use @mplungjan answer to have something working. But you still don’t know what went wrong.
The problem is you only save you last message. So in your case ‘test’ does not contain ‘Not Set’. You could break to end your loop and you could make your default text ‘Not found’ and only adjust it when you have your condition hit.
I want to emphasize this is not the best code but it is your code adjusted minimal to make it work.