M
M
mousesport2018-02-28 00:18:36
JavaScript
mousesport, 2018-02-28 00:18:36

How to get data from json array in JavaScript?

Good day!
tell me how to get the last30m value for the desired wor (for example, alesh.3)?

{
  "code": 0,
  "message": "ok",
  "data": {
    "rows": [
      {
        "wor": "alesh.1",
        "last10m": "0",
        "last30m": "10",
        "last1h": "0",
        "last1d": "0",
        "prev10m": "0",
        "prev30m": "0",
        "prev1h": "0",
        "prev1d": "0",
        "accepted": "5996544",
        "stale": "49152",
        "dupelicate": "0",
        "other": "0"
      },
      {
        "wor": "alesh.2",
        "last10m": "0",
        "last30m": "20",
        "last1h": "0",
        "last1d": "0",
        "prev10m": "0",
        "prev30m": "0",
        "prev1h": "0",
        "prev1d": "0",
        "accepted": "0",
        "stale": "0",
        "dupelicate": "0",
        "other": "0"
      },
      {
        "wor": "alesh.3",
        "last10m": "0",
        "last30m": "30",
        "last1h": "0",
        "last1d": "0",
        "prev10m": "0",
        "prev30m": "0",
        "prev1h": "0",
        "prev1d": "0",
        "accepted": "0",
        "stale": "0",
        "dupelicate": "0",
        "other": "0"
      }
    ],
    "page": 1,
    "totalPage": 1,
    "pageSize": 10,
    "totalRecord": 3
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-28
@mousesport

ES5 :

var wor = 'alesh.3';
var key = 'last30m';

var record = json.data.rows.find(function(el) {
  return el.wor === wor;
});

var value = record ? record[key] : null;

ES6 :
const wor = 'alesh.3';
const key = 'last30m';

const { data: { rows } } = json;

const record = rows.find(el => el.wor === wor);
const value = record ? record[key] : null;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question