在对象数组中搜索特定属性并返回布尔值

发布于 2025-01-09 08:43:36 字数 910 浏览 0 评论 0原文

假设我有一个对象数组

    let arr = [
  {
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];

和一个对象

  let obj = {
    name: "bill",
    age: 55
  }

,我想搜索所有 arr 对象以查找与 obj.age 年龄相同的任何人,并根据是否返回一个布尔值它是否包含相同的属性。

我显然可以这样做:

let find = arr.filter(i => i.age === obj.age);
let bool = find.length > 0 && true;

但是有没有办法调用一个方法(lodash、plain js 或其他)来通过设计来获得这个,如 method(arr,obj.age) //returns true

let arr = [
  {
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];

let obj = {
  name: "bill",
  age: 55
}

let find = arr.filter(i => i.age === obj.age);
let bool = find.length > 0 && true;

console.log(bool)

lets say i have an array of objects

    let arr = [
  {
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];

and an object

  let obj = {
    name: "bill",
    age: 55
  }

and i want to search all arr objects to find anyone with the same age as obj.age and return a boolean depending whether it includes a same property or not.

i can obviously do :

let find = arr.filter(i => i.age === obj.age);
let bool = find.length > 0 && true;

but is there a way to call a method (lodash,plain js or whatever) to just get this by design like method(arr,obj.age) //returns true ?

let arr = [
  {
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];

let obj = {
  name: "bill",
  age: 55
}

let find = arr.filter(i => i.age === obj.age);
let bool = find.length > 0 && true;

console.log(bool)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轮廓§ 2025-01-16 08:43:36

您可以使用一些。所以基本上,如果数组中的任何对象属性与所需值匹配,它将返回一个布尔值

let arr = [{
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];
let obj = {
  name: "bill",
  age: 55
}

const val = arr.some(item => item.age === obj.age);
console.log(val)

You can use some. So basically it will return a boolean value if any of the object property in array matches the required value

let arr = [{
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];
let obj = {
  name: "bill",
  age: 55
}

const val = arr.some(item => item.age === obj.age);
console.log(val)

桃扇骨 2025-01-16 08:43:36

我们可以通过两种方式做到这一点。我们可以使用 some 函数直接获取布尔值,或者通过 find 函数,我们可以使用 !! 将其转换为布尔值操作员。

let arr = [{
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];
let obj = {
  name: "bill",
  age: 55
}

const val1 = arr.some(item => item.age === obj.age);
console.log(val1)
const val2 = !!arr.find(item => item.age === obj.age);
console.log(val2)

we can do it in two ways. we can use the some function and get the boolean directly or by find function, we can convert it to a boolean using !! operator.

let arr = [{
    name: "john",
    age: 22
  },
  {
    name: "george",
    age: 55
  }
];
let obj = {
  name: "bill",
  age: 55
}

const val1 = arr.some(item => item.age === obj.age);
console.log(val1)
const val2 = !!arr.find(item => item.age === obj.age);
console.log(val2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文