0

I am working on an array which is like this:

var arr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

Now i want to compare the floor items here. If all the floor value is same, then it should return true and if there are multiple values like '1' & '2' in this array, it should return false.

Here's what i have tried so far

var arr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

for (let i=0; i< arr.length; i++){
    for (let j=1; j< arr.length; j++){
       if( arr[i].floor !== arr[j].floor){
            console.log('found');
       }else{
            console.log('Not found');
       }
    }
}

I know this is nowhere close to the solution. But i'm stuck.

5
  • 1
    You might be looking for Array.prototype.every() Commented Jan 6, 2022 at 15:48
  • 1
    what's the expected output? Commented Jan 6, 2022 at 15:54
  • @MajedBadawi a boolean Commented Jan 6, 2022 at 15:58
  • 1
    Posting a question and then significantly changing the posted code is a waste of people's time. Commented Jan 6, 2022 at 16:04
  • @MajedBadawi well... now the OP wants a string value. Commented Jan 6, 2022 at 16:07

4 Answers 4

2

An initial naive approach.

  • convert the check into a function so it can be reused
  • extract the floor property of the first item using Destructuring assignment and rename it to firstFloor
  • Use Array.prototype.every() to check if all the items meet a certain criteria

const allSameFloors = (array = []) => {
  const [{
    floor: firstFloor
  }] = array;
  return array.every(({
    floor
  }) => floor === firstFloor);
};

console.log(
  allSameFloors(
    [{
        floor: '1',
        id: '10165',
        label: 'Elutuba/Köök'
      },
      {
        floor: '1',
        id: '10166',
        label: 'Tuba 1'
      },
      {
        floor: '1',
        id: '10167',
        label: 'Vannituba'
      },
      {
        floor: '2',
        id: '10167',
        label: 'Vannituba'
      }
    ]
  )
);

console.log(
  allSameFloors(
    [{
        floor: '1',
        id: '10165',
        label: 'Elutuba/Köök'
      },
      {
        floor: '1',
        id: '10166',
        label: 'Tuba 1'
      },
      {
        floor: '1',
        id: '10167',
        label: 'Vannituba'
      },
      {
        floor: '1',
        id: '10167',
        label: 'Vannituba'
      }
    ]
  )
);

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

Comments

1

There more than likely is a better way of doing this but here is a quick solution.

function FloorSameValue(arr){
  values = [];
  for (obj of arr){
    if (!values.includes(obj.floor)){
      values.push(obj.floor);
    }
  }
  return values.length === 1;
}

var arr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

console.log(FloorSameValue(arr))
//false

Comments

0

there's many ways to achieve this actually, and the answer after me probably will provide better answer, so I just give you one in case you are in hurry

reference for array.every : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

// empty array always return true so need arr.length logic if we use array.every + empty array + want to return false for empty array, check more : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
const isFloorSame = (arr) => arr.length ? arr.every(({ floor }, i) => {
  if (i === 0) return true // no previous value to compare
  return floor === arr[i - 1].floor
}) : false;

const differentFloorArr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

const sameFloorArr = [
  {floor: '2', id: '10165', label: 'Elutuba/Köök'},
  {floor: '2', id: '10166', label: 'Tuba 1'},
  {floor: '2', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

console.log('differentFloorArr', isFloorSame(differentFloorArr));
console.log('sameFloorArr', isFloorSame(sameFloorArr));
console.log('emptyArray', isFloorSame([]));

Comments

0
for( var i = 0 ; i < arr.length;i++)
{
    var myval = arr[i].floor;
    for (var j = i+1 ; j < arr.length ; j++)
    {
        if (arr[i].floor !== arr[j].floor)
        {          
            alert("false");
        }
    }
}

    enter code here

2 Comments

While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please include an explanation for your code, as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
you are right , comment for solution is good and help to understand it, I try to explain my solution for better understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.