var answersAll=[ { "id":"13807146f85c4fdcb00206be6f596a7c", "questionType":3, "answerContent":["adsfdsfasfda"] }, { "id":"dsfdsafa", "questionType":3, "answerContent":["asdf"] }, { "id":"faadsfa", "questionType":3, "answerContent":["adsfadsfa"] }, { "id":"dfasfasdf", "questionType":3, "answerContent":["dasfdsfdfdsa"] } ]
function findElem(array, attr, val) { for (var i = 0; i < array.length; i++) { if (array[i][attr] == val) { return i; //返回当前索引值 } } return -1; };
let hasIdFlag = findElem(answersAll, 'id', '13807146f85c4fdcb00206be6f596a7c');
console.log(hasIdFlag)
// ———————————————— // 版权声明:本文为CSDN博主「mossbaoo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 // 原文链接:https://blog.csdn.net/mossbaoo/article/details/113241678 // for (var i = 0; i < answersAll.length; i++) { // if (answersAll[i][attr] == "13807146f85c4fdcb00206be6f596a7c") { // flag=i // } // }