E
E
Erl2020-07-19 07:38:49
JavaScript
Erl, 2020-07-19 07:38:49

How not to produce cycles?

There is a JSON snippet

JSON

{
"ammo": [
{
"caliber": "11,43x23",
"items": [
{
"name": ".45 FMJ",
"caliber": "11,43x23",
"damage": "72",
"arm_pentra": "19",
"arm_damage": "36%",
"bullet_frag": "1%",
"speed": "285",
"ricochet": "7%"
},
{
"name": ".45 RIP",
"caliber": "11,43x23",
"damage": "72",
"arm_pentra": "19",
"arm_damage": "36%",
"bullet_frag": "1%",
"speed": "285",
"ricochet": "7%"
}
]
},
{
"caliber": "12,7x108",
"items": [
{
"name": "12.7Б3T",
"caliber": "12,7x108",
"damage": "199",
"arm_pentra": "80",
"arm_damage": "95%",
"bullet_frag": "17%",
"speed": "818",
"ricochet": "38%"
},
{
"name": "Б-32",
"caliber": "12,7x108",
"damage": "182",
"arm_pentra": "88",
"arm_damage": "88%",
"bullet_frag": "17%",
"speed": "818",
"ricochet": "38%"
}
]
},
{

1) In order to get the name, damage and other data, I write 2 for loops
<div v-for="ammo in info" :key="ammo.caliber">
        <div v-for="ammo in ammo.items" :key="ammo.name">

schematically, * now it's all in the vue components *, so it's easier to see, is it possible to do this somehow in 1 cycle? Or is it ok to do it in 2 cycles?
2) as noted from json, in caliber, there are items, in one caliber there are several items (cartridges), but in order to also display caliber information, I additionally entered caliber in items, since I could not get the name of the caliber in the second cycle , but I think that this is not correct, how can I get the caliber property when displaying name, damage, etc.?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alekseyHunter, 2020-07-19
@alekseyHunter

If you don't want to get an indexing error - no way.
If you want - you decide on the forehead info[0].items[0].name.
Tip - do not optimize ahead of time. Here there will be gags in productivity, then already analyze.
There is no difference in 1 or 2 cycles on the minimum number of elements.

X
xmoonlight, 2020-07-21
@xmoonlight

json to a string and then regex what you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question