1

I found a nice example on here showing how to look through arrayObjects with a condition but I have a question.

As is stands its console.logging every time it the condition is false. Is it possible to only console.log once when its finished looping through everything.

var arrayObjects = [{
    "building": "A",
    "status": "good"
  },
  {
    "building": "B",
    "status": "horrible"
  }
];

for (var i = 0; i < arrayObjects.length; i++) {
  console.log(arrayObjects[i]);

  for (key in arrayObjects[i]) {

    if (key == "status" && arrayObjects[i][key] == "good") {

      console.log(key + "->" + arrayObjects[i][key]);
    } else {
      console.log("nothing found");
    }
  }
}

4
  • yes, just add an extra variable to that keeps track of whether you found any item or not Commented Oct 23, 2021 at 19:01
  • can you give me an example please Commented Oct 23, 2021 at 19:03
  • Create a new array of the output (what you would normally log) After the loops, log the array. Commented Oct 23, 2021 at 19:07
  • Do you need print all objects with they statuses, all with good status or only first object with good status? Plaese, add output as what you expect. Commented Oct 23, 2021 at 19:25

5 Answers 5

1

Simply use .length with if condition.

var arrayObjects = [{
    "building": "A",
    "status": "good"
  },
  {
    "building": "B",
    "status": "horrible"
  }
];

for (var i = 0; i < arrayObjects.length; i++) {
  console.log(arrayObjects[i]);

  if( i === arrayObjects.length-1 ) {
      console.log("nothing found");
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

Using the length with the if condition appeared to be the easiest one for me to understand the logic so I used this. Thanks to all for your input, very much appreciated
But nothing found will print anyway. There is no condition on object.status is it good or horrible
1

I'm assuming that you want it to print Nothing found when nothing's really found, not even a single thing..

Then, you can try this.


var arrayObjects = [{"building":"A", "status":"good"}, 
{"building":"B","status":"horrible"}];

var isFound = false;

 for (var i=0; i< arrayObjects.length; i++) {
  console.log(arrayObjects[i]);

   for(key in arrayObjects[i]) {

  if (key == "status" && arrayObjects[i][key] == "good") {
      isFound = true
      console.log(key + "->" + arrayObjects[i][key]);
  }
 }
}

if (isFound === false){
      console.log("nothing found");
  }

Comments

1

You can use the some or filter method of array.

var arrayObjects = [{"building":"A", "status":"good"}, 
{"building":"B","status":"horrible"}];

const found = arrayObjects.some(it => it.status === 'good')
if (found) {
  console.log('found')
}

const items = arrayObjects.filter(it => it.status === 'good')
if (items.length) {
  console.log('found')
}
 

Comments

1

If you're willing to refactor you code, you can save on time complexity by using just one loop with Array.reduce()

var arrayObjects = [{
    "building": "A",
    "status": "good"
  },
  {
    "building": "B",
    "status": "horrible"
  }
];
const foundKeys = arrayObjects.reduce((bool, key) => {
    console.log(key)
    if (key.status === "good") {
      console.log("status ->", key.status);
      bool = true
    }
    return bool 
}, false)

if (!foundKeys) {
    console.log("Nothing found")
}

Comments

0

Another declarative way solution:

const arrayObjects = [
  { "building": "A", "status": "good" },
  { "building": "B", "status": "horrible" },
];

const checkCondition = (arr, key = 'status', value ='good') => {
  const result = arr.find((obj) => obj[key] === value);
  return result 
    ? `${key} -> ${result[key]}` 
    : "nothing found";
};

console.log(checkCondition(arrayObjects));                  //status -> good
console.log(checkCondition(arrayObjects, 'building', 'B')); //building -> B
console.log(checkCondition(arrayObjects, 'building', 'C')); //nothing found

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.