如何检查数组是否包含特定对象

时间:2018-08-05 14:40:00

标签: javascript arrays javascript-objects indexof

我正在尝试检查对象数组是否包含特定对象或具有特定属性的对象。

var mainArray = [
{name:"Yahya", age:"29"},
{name:"Ahmed", age:"19"},
{name:"Mohamed", age:"10"},
{name:"Ali", age:"32"},
{name:"Mona", age:"25"},
{name:"Shady", age:"62"},
{name:"Reem", age:"11"},
{name:"Marwa", age:"52"}
]

var myObject = {name:"Yahya", age:"29"};

function check() {
if (mainArray.indexOf(myObject) > -1) {
  console.log("true")
  return true;
} else {
  console.log('false')
  return false;
}
};
<button onClick="check()">Check</button>

但是这里的对象与数组对象之一相同。但它返回false。 我尝试过包含,但也不起作用。

4 个答案:

答案 0 :(得分:2)

如果您希望检查对象数组是否包含该特定对象,则可以使用 x = (x*x) % p;

Array.some

您还可以使用var mainArray = [{ name: "Yahya", age: "29" }, { name: "Ahmed", age: "19" }, { name: "Mohamed", age: "10" }, { name: "Ali", age: "32" }, { name: "Mona", age: "25" }, { name: "Shady", age: "62" }, { name: "Reem", age: "11" }, { name: "Marwa", age: "52" } ] var myObject = { name: "Yahya", age: "29" }; let ans = mainArray.some(function(arrVal) { return myObject.name === arrVal.name; }); console.log(ans)方法。在这种情况下,它将返回匹配元素的数组。检查返回的数组的长度,以验证它是否包含所需的元素

filter

答案 1 :(得分:1)

Array提供了一种findIndex方法,我们可以像下面这样简单地做

var mainArray = [
  {name:"Yahya", age:"29"},
  {name:"Ahmed", age:"19"},
  {name:"Mohamed", age:"10"},
  {name:"Ali", age:"32"},
  {name:"Mona", age:"25"},
  {name:"Shady", age:"62"},
  {name:"Reem", age:"11"},
  {name:"Marwa", age:"52"}
]

var myObject = {name:"Yahya", age:"29"};

function check() {
  if (mainArray.findIndex(obj => obj.name == myObject.name && obj.age == myObject.age) > -1) {
    console.log("true")
    return true;
  } else {
    console.log('false')
    return false;
  }
};
<button onClick="check()">Check</button>

答案 2 :(得分:0)

您可以检查该数组中的some个元素是否与字符串化后要检查的对象相同,如下所示:

mainArray.some(e => JSON.stringify(e) === JSON.stringify(myObject))

function check() {
 console.log(mainArray.some(e => JSON.stringify(e) === JSON.stringify(myObject)))
};


let mainArray = [{
    name: "Yahya",
    age: "29"
  },
  {
    name: "Ahmed",
    age: "19"
  },
  {
    name: "Mohamed",
    age: "10"
  },
  {
    name: "Ali",
    age: "32"
  },
  {
    name: "Mona",
    age: "25"
  },
  {
    name: "Shady",
    age: "62"
  },
  {
    name: "Reem",
    age: "11"
  },
  {
    name: "Marwa",
    age: "52"
  }
]

let myObject = {
  name: "Yahya",
  age: "29"
};
<button onClick="check()">Check myObject</button>

答案 3 :(得分:0)

indexOf将尝试使用基本比较机制在集合中查找对象。尝试运行: {name:"Yahya", age:"29"} === {name:"Yahya", age:"29"} 在javascript控制台中,您将得到false,因为这是使用相同的字符串文字创建的2个不同的对象。

我相信您通常应该阅读有关OOP中对象的更多信息。

要检查集合是否包含提供了tester函数的元素,请使用Array.prototype.some():

var mainArray = [
    {name:"Yahya", age:"29"},
    {name:"Ahmed", age:"19"},
    {name:"Mohamed", age:"10"},
    {name:"Ali", age:"32"},
    {name:"Mona", age:"25"},
    {name:"Shady", age:"62"},
    {name:"Reem", age:"11"},
    {name:"Marwa", age:"52"}
];

var myObject = {name: "Yahya", age: "29"};

function check(object) {
    return mainArray.some(function(element) {
        return element.name === object.name && element.age === object.age;
    })
}

console.log(check(myObject));

请注意,比较是使用“ ===”完成的,如果您将mainArray集合中的年龄设置为字符串,并将myObject中的年龄设置为数字,则将“ ==”用于年龄比较。