skip to Main Content

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


  1. 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.

    var results = [
      ['(not set)', '87793', '1261928.2330840048'],
      ['(other)', '1146', '28096.821814000003'],
      ['test', '22', '215.958067']
    ];
    
    var notSetFound = false;
    
    for (var i = 0; i < results.length; i++) {
      console.log(results[i][0]);
    
      if (results[i][0] === '(not set)' || results[i][0] === '') {
        notSetFound = true;
        break; // Stop the loop as soon as '(not set)' is found
      }
    }
    
    if (notSetFound) {
      console.log('Not Set Data is there');
    } else {
      console.log('Not Set Data is NOT found');
    }
    Login or Signup to reply.
  2. You can use some

    var results = [
      ['(not set)', '87793', '1261928.2330840048'],
      ['(other)', '1146', '28096.821814000003'],
      ['test', '22', '215.958067']
    
    ]
    var final = { status: `Not set${
      results.some(arr => arr.includes('(not set)')) ? ' ' : ' not ' }found`
    };
    console.log(final)
    Login or Signup to reply.
  3. 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.

    var results = [
      ['(not set)', '87793', '1261928.2330840048'],
      ['(other)', '1146', '28096.821814000003'],
      ['test', '22', '215.958067']
    
    ]
    var final = {};
    // Create a default message
    final.status = 'Not Set is NOT found';
    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';
        // We found it so no need to look further
        break; 
      }
    }
    console.log(final)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search