Home > Software design >  Filter and get data from JSON
Filter and get data from JSON

Time:10-20

How to get the data value from the list, the size of the array, the main thing is not through the index, because the order of the arrays can change and I can get specific data from the code === "size". Unfortunately, the structure cannot be changed. It came to mind only through the filter, by index, but it is impossible

The result should be 100 150

https://jsoneditoronline.org/#left=cloud.b10638604c214b189f87747414e06035

[
  [
    "color",
    {
      "name": "Цвет",
      "code": "color",
      "list": [
        {
          "value": "Зеленый"
        },
        {
          "value": "Красный"
        }
      ]
    }
  ],
  [
    "size",
    {
      "name": "Размер",
      "code": "size",
      "list": [
        {
          "value": "100"
        },
        {
          "value": "150"
        }
      ]
    }
  ]
]

CodePudding user response:

This data structure is terrible, but a quick fix could be something like this

 const data = [
    
      [
        "color",
        {
          "name": "Цвет",
          "code": "color",
          "list": [
            {
              "value": "Зеленый"
            },
            {
              "value": "Красный"
            }
          ]
        }
      ],
      [
        "size",
        {
          "name": "Размер",
          "code": "size",
          "list": [
            {
              "value": "100"
            },
            {
              "value": "150"
            }
          ]
        }
      ]
    ]
    
    const size = data.filter(element => element[1].code === 'size')[0][1].list.map(element => element.value)
    
    console.log(size)

  • Related