skip to Main Content

I have an array object. I want to check all ‘selected’ property is true or not. How to check this in javascript.

Demo: https://stackblitz.com/edit/js-xmhxzk?file=index.js

var arr1 = [
    { id:"1", selected: true },
    { id:"2", selected: true },
    { id:"3", selected: true }
];

var arr2 = [
    { id:"1", selected: true },
    { id:"2", selected: false },
    { id:"3", selected: true }
];

checkArr(arr1);
checkArr(arr2);

function checkArr(arr) {
    let i = arr.length;
    while (i--) {
        if (arr[i].selected === true) {
            alert("all selected is true from this array");
        }  
    else if(arr[i].selected === true){
            console.log("At least one selected");
          }
    else {
            alert("No..All are not selected");
        }
    }
}

4

Answers


  1. You can use a combination of every and some function to achieve your result easily. Something like:

    const arr1 = [
      { id: "1", selected: true },
      { id: "2", selected: true },
      { id: "3", selected: true }
    ];
    
    const arr2 = [
      { id: "1", selected: true },
      { id: "2", selected: false },
      { id: "3", selected: true }
    ];
    
    checkArr(arr1);
    checkArr(arr2);
    
    function checkArr(arr) {
      const allSelected = arr.every(item => item.selected);
      const atLeastOneSelected = arr.some(item => item.selected);
    
      if (allSelected) {
        alert("All selected is true from this array");
      } else if (atLeastOneSelected) {
        alert("At least one selected");
      } else {
        alert("No..All are not selected");
      }
    }
    Login or Signup to reply.
  2. You could keep count of how many elements are selected. If that count equals the length of the array, then all were selected.

    var arr1 = [
        { id:"1", selected: true },
        { id:"2", selected: true },
        { id:"3", selected: true }
    ];
    
    var arr2 = [
        { id:"1", selected: true },
        { id:"2", selected: false },
        { id:"3", selected: true }
    ];
    
    var arr3 = [
        { id:"1", selected: false },
        { id:"2", selected: false },
        { id:"3", selected: false }
    ];
    
    checkArr(arr1);
    checkArr(arr2);
    checkArr(arr3);
    
    function checkArr(arr) {
        let i = arr.length;
        let selectedCount = 0;
        while (i--) {
          if (arr[i].selected === true) {
            selectedCount++;
          }
        }
        if (selectedCount === arr.length) {
          console.log("All are selected in this array");
        } else if (selectedCount > 0) {
          console.log("At least one selected");
        } else {
          console.log("None selected");
        }
    }
    Login or Signup to reply.
  3. For checking all "selected" property is true then you can use "every" method. And if you want to at least one selected property is set to true then use "some".

    var arr1 = [
        { id: "1", selected: true },
        { id: "2", selected: true },
        { id: "3", selected: true }
    ];
    
    var arr2 = [
        { id: "1", selected: true },
        { id: "2", selected: false },
        { id: "3", selected: true }
    ];
    
    checkArr(arr1);
    checkArr(arr2);
    
    function checkArr(array) {
        let allItemSelected = array.every(arrayData => arrayData.selected === true);
        let atLeastOneSelected = array.some(arrayData => arrayData.selected === true);
    
        if (allItemSelected) {
            console.log("All Item selected from array");
        } else if (atLeastOneSelected) {
            console.log("At least one item selected");
        } else {
            console.log("No.. All are not selected");
        }
    }
    
    Login or Signup to reply.
  4. In JavaScript, you can check if all the objects in an array have a specific property set to true by using the every method along with a callback function. Here’s an example:

    // Sample array of objects
    const arrayOfObjects = [
      { id: 1, selected: true },
      { id: 2, selected: true },
      { id: 3, selected: false },
      { id: 4, selected: true }
    ];
    
    // Check if all 'selected' properties are true
    const allSelected = arrayOfObjects.every(obj => obj.selected === true);
    
    // Output the result
    console.log(allSelected);  // Output: false (since the third object has 'selected' set to false)
    
    

    In the example above, the every method is used to check if the selected property of all objects is equal to true. The every method returns true if the callback function returns true for every element in the array; otherwise, it returns false.

    You can simplify the code by directly using the boolean value of the property, like this:

    const allSelected = arrayOfObjects.every(obj => obj.selected);
    
    

    This checks if all objects have the selected property set to true. If any object has selected set to false or if the property is not present in any object, the result will be false.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search