0

hi this is my array

this.testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];

i want to check whether officename sun is there or not. I tried used include
this.testArray.includes("sun") but this will work for one dimension array( it will return true). how if I want to use it for this multidimension array. it always return false.

** this.testArray.includes("sun") This will work with this array
**

this.testArray = ["sun","moon","stars" }];

it will return true it exist. but it is not working with my array the up one.

1
  • Can you give the sample multidimensional array you are using because the one you provided is not multidimensional Commented Apr 7, 2017 at 8:39

4 Answers 4

2

Your array is not multidimensional. However you can do it like :

testArray.find(function(i){
   return (i.officename === 'sun');
});
Sign up to request clarification or add additional context in comments.

4 Comments

i need to do it in my html, if it return true then the checkbox will be checked
you can create a function in your controller and call it from html. See this working fiddle
o i see, ok let me try
@user3431310 : sure.. and if it helps please accept as answer.
1

In case you need a filter directly

| filter:{officename:'sun'}

or negative

| filter:{officename:'!sun'}

Comments

0

Simply you can check value include or not by using underscorejs find function

this.testArra = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }]

var temp =  _.find(this.testArra, function (o) { return o.officename==='sun2'; })

if temp "undefined" value not include, else value include

1 Comment

i need to do it in my html, if it return true then the checkbox will be checked
0

you can use find method in javascript

ES6 demo

var testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];

var arr = testArray.find(o=> o.officename === "sun");

console.log(arr)

ES5 demo

var testArray = [{ "office_id": "1", "officename": "sun" }, { "office_id": "2", "officename": "moon" }];

var arr = testArray.find(function(o){
   return  o.officename === "sun"
});

console.log(arr)

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.