javascript - Get one item from an array based on a condition -
i have array different values (in json structure), need 1 item on array based on value.
this did far:
var orders= [{ "id": 1, "order": "11" }, { "id": 2, "order": "22" }, { "id": 3, "order": "33" }]; var order = $.grep(orders, function (e) { return e.order === '22'; })[0]; var orderid = order ? order.id : 0;
is simplest way it?
with es6, use array#find
var orders= [{ id: 1, order: "11" }, { id: 2, order: "22" }, { id: 3, order: "33" }], order = orders.find(o => o.order === "22"), orderid = order ? order.id : 0; console.log(orderid);
Comments
Post a Comment