P
P
Pavel Chuev2015-10-03 15:14:33
JavaScript
Pavel Chuev, 2015-10-03 15:14:33

How to parse a multi-dimensional JS array and pull out the desired element from it?

Let's say we have an array like this:

[{"i_id":"2223", "i_post":"qwerty", "i_descriptions":[{"type":"html","value":"\r\n"},{"type":"html","value":" "},{"type":"html","value":"\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0442\u0438\u0440\u0430\u0436\u0430 #48","color":"99ccff"},{"type":"html","value":" "}], "ui_status":"4", "ui_bid":"12414124125"}]

and there can be many of them, but with different meanings. How to iterate through them and execute the desired query in turn with each non-zero ui_bid value?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-10-03
@AllDecay

function my_func(ui){
  //Чего-то делаем, к примеру:
  alert(ui);
}

var data = [
  {"i_id":"2223", "i_post":"qwerty", "i_descriptions": [
    {"type":"html","value":"\r\n"},
    {"type":"html","value":" "},
    {"type":"html","value":"\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0442\u0438\u0440\u0430\u0436\u0430 #48","color":"99ccff"},
    {"type":"html","value":" "}
  ], "ui_status":"4", "ui_bid":"12414124125"},
  {"i_id":"2228", "i_post":"qwerty", "i_descriptions": [
    {"type":"html","value":"\r\n"},
    {"type":"html","value":" "},
    {"type":"html","value":"\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0442\u0438\u0440\u0430\u0436\u0430 #48","color":"99ccff"},
    {"type":"html","value":" "}
  ], "ui_status":"4", "ui_bid":"0"}
];

data.forEach(function(e){
   var ubid = e.ui_bid;
   +ubid&&my_func(ubid);
});

And please, in the future, you do not need to give unformatted code, we are people, not machines.

P
pantsarny, 2015-10-03
@pantsarny

var data = [{"i_id":"2223", "i_post":"qwerty", "i_descriptions":[{"type":"html","value":"\r\n"},{"type":"html","value":" "},{"type":"html","value":"\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0442\u0438\u0440\u0430\u0436\u0430 #48","color":"99ccff"},{"type":"html","value":" "}], "ui_status":"4", "ui_bid":"12414124125"}];

for (var i in data) {
  if (data[i].ui_bid != 0) {
    // do something
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question