soubhagya pradhan
soubhagya pradhan

Reputation: 547

Json customization issue in javascript

handleHitDrop = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    let hit_drop = arr[i]["Z" + (i + 1)]["Drop"][0] + arr[i]["Z" + (i + 1)]["Hit"][0]
    arr[i]["Z" + (i + 1)] = hit_drop
  }
  console.log(arr)
}



data = [{
  "Z1": {
    "Drop": [
      0
    ],
    "Hit": [
      0
    ]
  },
  "Z2": {
    "Drop": [
      0
    ],
    "Hit": [
      1
    ]
  },
  "Z3": {
    "Drop": [
      1
    ],
    "Hit": [
      1
    ]
  }
}]



handleHitDrop(data)

expecting result:

[
  {
    "Z1": 0,
    "Z2": 1,
    "Z3": 2
  }
]

Here i am trying to customize my existing json. trying to add first element of Drop and Hit and assigning to parent key.

Here is how am trying to do but, it is only doing for first one (Z1)

Upvotes: 0

Views: 29

Answers (3)

Mohammad Faisal
Mohammad Faisal

Reputation: 2432

handleHitDrop = (arr) => {
  for (let obj of arr) {
    Object.keys(obj).map(key => {
      let dropCount = (obj[key]["Drop"] || [])[0] || 0;
      let hitCount = (obj[key]["Hit"] || [])[0] || 0;
      obj[key] = dropCount + hitCount;
    })
  }
  console.log(arr);
  return arr;
}



data = [{
  "Z1": {
    "Drop": [
      0
    ],
    "Hit": [
      0
    ]
  },
  "Z2": {
    "Drop": [
      0
    ],
    "Hit": [
      1
    ]
  },
  "Z3": {
    "Drop": [
      1
    ],
    "Hit": [
      1
    ]
  }
}]



handleHitDrop(data)

Upvotes: 0

trincot
trincot

Reputation: 350996

You should avoid making those array properties with literal "Z" and sequence number. Just iterate over the properties. Object.entries and Object.fromEntries can be used for a nice functional approach:

let data = [{"Z1": {"Drop": [0],"Hit": [0]},"Z2": {"Drop": [0],"Hit": [1]},"Z3": {"Drop": [1],"Hit": [1]}}]

data = data.map(obj => Object.fromEntries(
    Object.entries(obj).map(([z, {Drop, Hit}]) => [z, Drop[0] + Hit[0]])
));

console.log(data);

The problem with your own code, is that i is the index in the overall array, not the Z properties: for that you need another, nested loop.

Upvotes: 1

blex
blex

Reputation: 25659

Here is a way to do it:

const handleHitDrop = (arr) => {
  return arr.map(obj => {
    // For each key (Z1, Z2...)
    return Object.keys(obj).reduce((res, key) => {
      // Add that key to the result, with the sum as a value
      return {
        ...res,
        [key]: sumArray(obj[key].Drop) + sumArray(obj[key].Hit)
      };
    }, {});
  });
}

// Returns the sum of all items in an Array of Numbers
function sumArray(arr) {
  return arr.reduce((sum, v) => sum + v, 0);
}

const data = [{
  "Z1": {
    "Drop": [0],
    "Hit": [0]
  },
  "Z2": {
    "Drop": [0],
    "Hit": [1]
  },
  "Z3": {
    "Drop": [1],
    "Hit": [1]
  }
}]

console.log(handleHitDrop(data));

Upvotes: 0

Related Questions