5

I need to know if it's possible to use the array.every method on multidimensional arrays.

My array looks like this:

tabs=[
{label: string, icon: icon, routerLink: link},
{label: string, icon: icon, routerLink: link},
{label: string, icon: icon, routerLink: link}]

I need to know whether every LABEL inside tabs is unequal to a specific label. I'd really appreciate it, if you could offer a detailed answer, since I'm a newbie-programmer and want to understand what I'm doing! But feel free to answer anyway. :) Thanks so far!

EDIT: I'm using this method to add Tabs to my Tabmenu(ng2, primeng):

addTab(id: string) {
  if (id === 'linechart') {
    this.tab = {
      label: 'NW-Details',
      icon: 'fa-area-chart',
      routerLink: ['/nwdetails']
    }
    TABS.push(this.tab);
  }
  if (id === 'piechart') {
    this.tab = {
      label: 'IO-Details',
      icon: 'fa-pencil',
      routerLink: ['/iodetails']
    }
    TABS.push(this.tab)
  }
}

Whereas TABS is typeof MenuItem[] offered by primeng, tab is any.

Every time I doubleclick a chart, this one gets invoked an a new tab is pushed into my menu. Now I want to check wheter a tab with certain label is already opened, so that it does not open again. I tried using for loops combined with if

for (i = 0; i < TABS.length; i++) {
  if (TABS[i].label !== 'NW-Details') {
    this.tab = {
      label: 'NW - Details',
      icon: 'fa - area - chart'
      TABS.push(this.tab)
    }
  }

But this opens a new Tab every time it is unequal, so that actually more then one tab gets opened on dblclick if there are more tabs already opened.

9
  • 3
    tabls.every((v) => v.label !== someValue) or !tabls.some((v) => v.label === someValue) Commented Jun 22, 2016 at 8:11
  • 1
    Welcome to Stack Overflow! Please take the tour and read How to Ask to learn what we expect from questions here. Please be aware that we do not provide from-scratch coding service here. Please show us what you've tried already, how it failed and we might be able to help. Also, you don't have a multidimensional array, but an array of objects. Commented Jun 22, 2016 at 8:11
  • Can you explain what you are trying to achieve? Commented Jun 22, 2016 at 8:12
  • 1
    ES5 version: tabs.every(function(item){ return item.label !== matchString}) Commented Jun 22, 2016 at 8:13
  • You want to filter out the objects whose labels match to that given string? Commented Jun 22, 2016 at 8:18

2 Answers 2

7

You can use Array#every method.

tabls.every(function(v){
  return v.label !== someValue
})

Check MDN docs :

The every method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value (a value that becomes false when converted to a Boolean). If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.


or use Array#some method

!tabls.some(function(v){
  return v.label === someValue
})

Check MDN docs:

some() executes the callback function once for each element present in the array until it finds one where callback returns a truthy value (a value that becomes true when converted to a Boolean). If such an element is found, some() immediately returns true. Otherwise, some() returns false. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

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

3 Comments

Thanks for the explanation, worked just fine for me. And, even better, I understood what's happening. Is there any difference in efficiency between some and every method? They look quite similar, so I'd guess there isn't.
In my understanding, both would perform same. Difference is, array.some will return true on first true value. array.every will return false on first falsey value. When to use which? If your situation is all elements should satisfy certain condition, use Every. if your situation is anyone of values, then use Some.
@Faigjaz : both are different.............. every method will check that all return values are truthy... and some check any of them is true
0

Signature of Array functions is something like this:

Array.prototype.functionName = function(callback){
  for (var i = 0; i< this.length; i++){
    if(callback(this[i]))
      // do something.
    else
      // do something.
  }
}

It expects a callback which returns boolean value i.e. true or false(default).

every vs some

Though both can be used in a similar fashion, they should be used in a way they convey proper meaning.

var d = [1,2,3,4,5];
var valid = d.every(function(num){ return !isNaN(num); });
console.log(valid)

Above code means "valid if all elements are numbers". You can use .some but then that will change the meaning to "invalid if anyone is not number". You should use them based on the current context of code to improve readability.

References

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.