1

I have result.json that looks like this :

[
 {
  "id":"1",
  "label":"2017-03-30",
  "value":"1675000"
 },
 {
  "id":"2",
  "label":"2017-04-01",
  "value":"1440000"
 },
 {
  "id":"2",
  "label":"2017-04-02",
  "value":"830000"
 },
]

How can I get the specific items in json array .
for example I wanted to make an output for each "value" if

id=2 and label=2017-04-01
0

2 Answers 2

7
console.log(arr.filter(item => {
    return item.id === `2` && item.label === `2017-04-01`;
}));

recommended Improvement:

var arr=[{
   "id":"1",
   "label":"2017-03-30",
   "value":"1675000"
 },
 {
   "id":"2",
   "label":"2017-04-01",
   "value":"1440000"
 },
 {
   "id":"2",
   "label":"2017-04-02",
   "value":"830000"
  },
];
console.log(...arr.filter(item => item.id === `2` && item.label === `2017-04-01`));

If you just want the values:

console.log(...arr.filter(item => item.id === `2` && item.label === `2017-04-01`).map(e=>e.value));

http://jsbin.com/fimulelebi/edit?console

Sign up to request clarification or add additional context in comments.

4 Comments

That won't return value but entire object.
you could leave away the { return }
arr.filter(item => item.id === 2 && item.label === 2017-04-01)[0]
@Alex ive just edited your code, if you think its better you can take it over, if not you can dump it...
1

Use forEach method. Easy to use and navigate through items.

var json = [
  {
    "id": "1",
    "label": "2017-03-30",
    "value": "1675000"
  },
  {
    "id": "2",
    "label": "2017-04-01",
    "value": "1440000"
  },
  {
    "id": "2",
    "label": "2017-04-02",
    "value": "830000"
  },
];

json.forEach(item => {
  if(item.id == "2" && item.label == "2017-04-01") {
    console.log(item.value);
  }
})

2 Comments

how if the json is on external files (result.json )?how do we call it on js?
Using Ajax. But that's question not related with what you asked here, so you should write new one. Before you do that, read about Ajax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.