我想根据属性“id”的随机值显示一个对象。随机值必须介于1和数组中的对象数量之间。目前我有这个:
$(document).ready(function () {
var card = [
{
name: "string 1"
, value: "this"
, other: "that"
, id: 1
}
, {
name: "string 2"
, value: "this"
, other: "that"
, id: 2
}
];
var obj = card.filter(function (obj) {
var length = card.length();
var random = Math.floor(Math.random() * length) + 1
return obj.name === random;
})[0];
console.log(obj);
});