E
E
Erl2020-07-23 08:28:19
JavaScript
Erl, 2020-07-23 08:28:19

How to get a property in an array to sort?

piece of 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%",
                    "without_armor": [{
                        "min": "2",
                        "avg": "2",
                        "max": "2"
                    }],
                    "gen4": [{
                        "min": "22",
                        "avg": "22",
                        "max": "22"
                    }],
                    "zhuk6": [{
                        "min": "21",
                        "avg": "21",
                        "max": "21"
                    }]
                },
                {
                    "name": ".45 RIP",
                    "caliber": "11,43x23",
                    "damage": "72",
                    "arm_pentra": "19",
                    "arm_damage": "36%",
                    "bullet_frag": "1%",
                    "speed": "285",
                    "ricochet": "7%",
                    "without_armor": [{
                        "min": "1",
                        "avg": "1",
                        "max": "1"
                    }],
                    "gen4": [{
                        "min": "15",
                        "avg": "15",
                        "max": "15"
                    }],
                    "zhuk6": [{
                        "min": "15",
                        "avg": "15",
                        "max": "15"
                    }]
                }
            ]
        },
        {
            "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%",
                    "without_armor": [{
                        "min": "1",
                        "avg": "1",
                        "max": "1"
                    }],
                    "gen4": [{
                        "min": "1",
                        "avg": "1",
                        "max": "2"
                    }],
                    "zhuk6": [{
                        "min": "1",
                        "avg": "1",
                        "max": "2"
                    }]
                },
                {
                    "name": "Б-32",
                    "caliber": "12,7x108",
                    "damage": "182",
                    "arm_pentra": "88",
                    "arm_damage": "88%",
                    "bullet_frag": "17%",
                    "speed": "818",
                    "ricochet": "38%",
                    "without_armor": [{
                        "min": "1",
                        "avg": "1",
                        "max": "1"
                    }],
                    "gen4": [{
                        "min": "1",
                        "avg": "1",
                        "max": "2"
                    }],
                    "zhuk6": [{
                        "min": "1",
                        "avg": "1",
                        "max": "2"
                    }]
                }
            ]
        },



There is such a json file, I form a table and sort by caliber
sortByCaliber() {
      this.ammo_data.sort((a, b) => {
        a.caliber.localCompare(b.name)
      });
    },

But, how to sort by damage, because it is in the array and I cannot write
sortByDamage() {
      this.ammo_data.sort((a, b) => {
        return a.items.damage - b.items.damage;
      });
    },

Then how can one get to these properties?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2020-07-23
@NooBiToo

Convert the ammo array to a flat items array, something like:

let items = ammo.reduce((accum, el) => accum.concat(el.items), []);

and already sort it, filter it as you like.

V
Vladimir, 2020-07-23
@Casufi

a.items[0].damage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question