【问题标题】:how do I find 'abc' is in the array [{title:'ccc'},{title:'abc'},{title:'ddd',}]我如何找到'abc'在数组中 [{title:'ccc'},{title:'abc'},{title:'ddd',}]
【发布时间】:2019-06-10 09:00:14
【问题描述】:

我想知道 [{title:'ccc'},{title:'abc'},{title:'ddd'}] 中是否有 'abc'

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

//这个怎么做以及如何判断

【问题讨论】:

标签: javascript arrays object ecmascript-6


【解决方案1】:

最简单的答案是some

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

您还可以将includesflatMapObject.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);

没有flatMapflat 的版本(不太支持):

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 语法:

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);

【讨论】:

  • 你让这变得比它应该的更复杂。解决这个问题的最规范正确的方法是Array#some
  • OK @str 我会补充的。
【解决方案2】:

数组​.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);

【讨论】:

    【解决方案3】:

    您可以使用Array#find

    Array#find 将返回匹配项,如果未找到匹配项,则返回 undefined,因此您可以在 if 语句中使用结果。

    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';
    }
    

    【讨论】:

      【解决方案4】:
      var exist = b.find(function(element) {
        return element.title === a;
      });
      

      如果您知道属性标题是您想要看到的,这应该是您的解决方案

      【讨论】:

        【解决方案5】:
          for (i=0; i<b.length; i++){
               if(i.title == a){
               console.log('yes')
               }
          }
        

        【讨论】:

        • 请考虑更正您的代码,以便在满足条件时返回“yes”/“no”而不是记录“yes”
        【解决方案6】:
        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;
        }
        

        【讨论】:

          猜你喜欢
          • 2017-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-31
          • 2015-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多