-2

I wanted to map all the names and power of the objects and if he finds an array inside gadgets he would add +1 (i++), how would that be?

The list is much bigger, but I just show these two

  "list": [

    {
      "name": "SHELLY",
      "power": 10,
      "gadgets": [
        {
          "id": 23000255,
          "name": "FAST FORWARD"
        }
      ]
    },
    {
      "name": "COLT",
      "power": 7,
      "gadgets": [
        {
          "id": 23000273,
          "name": "SPEEDLOADER"
        },
        {
          "id": 23000319,
          "name": "SILVER BULLET"
        }
      ]
  ]
}
Dyoferzn
  • 31
  • 3

1 Answers1

0

A simple map should do it:

const data = {list:[{name:"SHELLY",power:10,gadgets:[{id:23000255,name:"FAST FORWARD"}]},{name:"COLT",power:7,gadgets:[{id:23000273,name:"SPEEDLOADER"},{id:23000319,name:"SILVER BULLET"}]}]};

const res = data.list.map(p => ({
  name: p.name,
  power: p.power + p.gadgets.length
}));

console.log(res);
blex
  • 24,941
  • 5
  • 39
  • 72