我有以下javascript对象
myData={my_ID: "NSOfe",his_ID: "AuJ"}
我想比较my_Data
,看看我是否在以下javascript对象(my_ID
)中有相同的myData_1
。如果有,它将在控制台上返回true。
myData1=[{my_ID: "NSOfe",his_ID: "suJ"},{my_ID: "NSOfew",his_ID: "kuJ"},{my_ID: "NSOfey",his_ID: "BuJ"}]
答案 0 :(得分:2)
然后比较对象的my_ID
属性:
console.log(myData.my_ID === myData1.my_ID); // true
对于许多比较,将您的项目放入数组并使用for循环来完成它们:
var dataArray = [ myData1, myData2, myData3 ];
for( var i = 0; i < dataArray.length; i++ )
console.log(myData.my_ID === dataArray[i].my_ID);
答案 1 :(得分:0)
您可以对数组进行字符串化,然后只需对其执行indexOf调用:
var myData=[{my_ID: "NSOfe",his_ID: "AuJ"},{my_ID: "NSOfe",his_ID: "AuJ"}];
var myDataString = JSON.stringify(myData);
console.log(myDataString.indexOf('"my_ID":"NSOfe"') !== -1);
console.log(myDataString.indexOf('"my_ID":"NSOfe2"') !== -1);
控制台输出:
true
false