-1

i want to know is there is a 'abc' in the [{title:'ccc'},{title:'abc'},{title:'ddd'}]

let a = 'abc'
let b = [{title:'ccc'},{title:'abc'},{title:'ddd'}]
if there is a in b{
   return 'yes'
} else {
   return 'no
}

//how can I do this and how to judge this

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
RtyXmd
  • 22
  • 4
  • Possible duplicate of [Best way to find if an item is in a JavaScript array?](https://stackoverflow.com/questions/143847/best-way-to-find-if-an-item-is-in-a-javascript-array) – Veverke Jun 10 '19 at 09:05
  • @Veverke I would disagree. The duplicate post would search for the whole entry within an array. The OP here is asking to match a part of the entry. – James Long Jun 10 '19 at 09:08
  • Possible duplicate of [How to determine if Javascript array contains an object with an attribute that equals a given value?](https://stackoverflow.com/questions/8217419/how-to-determine-if-javascript-array-contains-an-object-with-an-attribute-that-e) – str Jun 10 '19 at 09:15
  • @JamesLong: you are correct James, am removing my vote. – Veverke Jun 10 '19 at 13:38

6 Answers6

2

Array​.prototype​.some()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

var a = 'abc';
var b = [
  {value: 'def'},
  {value: 'abc'},
  {value: 'ghi'}
];
const result = b.some(x => x.value === a);
console.log(result);
Joey Gough
  • 2,753
  • 2
  • 21
  • 42
2

The simplest answer is some:

let a = 'abc'
let b = [{title:'ccc'},{title:'abc'},{title:'ddd'}];
let aInB = b.some(({ title }) => title == a);
console.log(aInB);

You could also use includes with flatMap and Object.values:

let a = 'abc'
let b = [{title:'ccc'},{title:'abc'},{title:'ddd'}];
let aInB = b.flatMap(Object.values).includes(a) ? "Yes" : "No"; 
console.log(aInB);

Version without flatMap or flat (not well supported):

let a = 'abc'
let b = [{title:'ccc'},{title:'abc'},{title:'ddd'}];
let aInB = b.map(Object.values).reduce((a, c) => a.concat(c)).includes(a) ? "Yes" : "No"; 
console.log(aInB);

ES5 syntax:

var a = 'abc'
var b = [{title:'ccc'},{title:'abc'},{title:'ddd'}];
var aInB = b.map(function(e) {
  return Object.keys(e).map(function(key) {
    return e[key];
  });
}).reduce(function(a, c) {
  return a.concat(c);
}).indexOf(a) > -1 ? "Yes" : "No";
console.log(aInB);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

You can use Array#find.

Array#find will return the matching item or undefined if it doesn't find a match, so you can use the result in your if statement.

let a = 'abc'
let b = [{title:'ccc'},{title:'abc'},{title:'ddd'}]
let c = b.find((d) => d.title === a);

if (c) {
    return 'yes';
} else {
    return 'no';
}
James Long
  • 4,629
  • 1
  • 20
  • 30
0
var exist = b.find(function(element) {
  return element.title === a;
});

If you know property title is what you want to see, this should be your solution

Ranjan Adhikari
  • 251
  • 1
  • 11
0
  for (i=0; i<b.length; i++){
       if(i.title == a){
       console.log('yes')
       }
  }
Noa
  • 424
  • 1
  • 3
  • 16
  • 1
    Please consider correcting your code so that it will return "yes"/"no" instead of logging "yes" when the condition if satisfied – barbsan Jun 10 '19 at 09:35
0
var a = 'ccc'
var b = [{title:'ccc'},{title:'abc'},{title:'ddd'}];

function presentAsTitle(a, b) {
    var flag = false;
    for (var i=0; i<=b.length; i++) {
        var item = b[i];
        if (item.title === a) {
            flag = true;
            break;
        }
    }
    return flag;
}
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34