0

I have this json file. How do I view items with version "A" only? Thank you very much.

[
    {
      "id": 1,
      "version": "A",
      "description": "description 01",
      "title": "title 01"
    },
    {
      "id": 2,
      "version": "B",
      "description": "description 02",
      "title": "title 02"
    },
    {
      "id": 3,
      "version": "A",
      "description": "description 03",
      "title": "title 03"
    }
]

I know how to write all values ​​but how to write only "A", I don't know.

PepikVaio
  • 21
  • 6
  • 1
    Does this answer your question? [Filter an array based on an object property](https://stackoverflow.com/questions/35231008/filter-an-array-based-on-an-object-property) – Shane Creedon Mar 29 '20 at 19:01

3 Answers3

2

If you want to just find items that matches the key (Eg. version), you can use find method like this:

let obj = 
[
    {
      "id": 1,
      "version": "A",
      "description": "description 01",
      "title": "title 01"
    },
    {
      "id": 2,
      "version": "B",
      "description": "description 02",
      "title": "title 02"
    },
    {
      "id": 3,
      "version": "A",
      "description": "description 03",
      "title": "title 03"
    }
];
let search =  "A"; //====> item to be searhed
let result = obj.find((item)=> item.version === search); // ===> Returns the value that satisfies the condition
console.log(result);
Mr Khan
  • 2,139
  • 1
  • 7
  • 22
2

Parsing JSON in QML is no different than parsing JSON in Javascript, because QML provides an environment based on ECMAScript with some modifications especially for QML.

So you can use the in-built JSON.parse() function like this:

var obj = '[
                {
                  "id": 1,
                  "version": "A",
                  "description": "description 01",
                  "title": "title 01"
                },
                {
                  "id": 2,
                  "version": "B",
                  "description": "description 02",
                  "title": "title 02"
                },
                {
                  "id": 3,
                  "version": "A",
                  "description": "description 03",
                  "title": "title 03"
                }
          ]'

var jsonData = JSON.parse(obj);

for (var i = 0; i < jsonData.length; i++) {
    console.log("Parse object i= " + i);
    if(jsonData[i].version === "B"){
        console.log(jsonData[i].id)
        console.log(jsonData[i].version)
        console.log(jsonData[i].description)
        console.log(jsonData[i].title)
    }
}
Adriano Campos
  • 1,121
  • 7
  • 14
  • 1
    Thank you very much :) I am very happy. It's very simple :) Just add this line to my code "if(jsonData[i].version === "B"){}" Thank you very much :) – PepikVaio Mar 30 '20 at 16:44
  • Do not forget to thumbs up the answers, when they match the request to get more visibility and help other people – Adriano Campos Apr 01 '20 at 09:24
0

You can use filter method

const items = [
    {
      "id": 1,
      "version": "A",
      "description": "description 01",
      "title": "title 01"
    },
    {
      "id": 2,
      "version": "B",
      "description": "description 02",
      "title": "title 02"
    },
    {
      "id": 3,
      "version": "A",
      "description": "description 03",
      "title": "title 03"
    }
];

const versionAItems = items.filter( item => item.version === 'A' )
console.log(versionAItems)
Alcadur
  • 681
  • 1
  • 8
  • 22