Home > Software engineering >  adding elements in array object
adding elements in array object

Time:10-02

i am working on a node js project in there i have to add some elements in a array object. i had figure out one way , it has a problem. it can't work with duplicate name's.

hare is the code

let arrayObject = [
  { name: "l2k", killPoint: 10 },
  { name: "l2kb", killPoint: 6 },
  { name: "gd", killPoint: 0 },
  { name: "l2kb", killPoint: 5 },
  { name: "gd", killPoint: 0 },
  { name: "l2k", killPoint: 4 },
  { name: "gd", killPoint: 0 },
  { name: "l2k", killPoint: 1 },
];

//conver ter using
let winCount = [1, 0, 0, 0, 0, 0];

let pointsystem = [12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0];
const positionAdd = (objects) => {
  const posRes = {};
  let i = 0;
  objects.forEach(({ name, killPoint }) => {
    posRes[name] = posRes[name] || {
      name,
      win: 0,
      positionPoint: 0,
      killPoint: 0,
    };
    posRes[name].win = winCount[i] || 0;
    posRes[name].positionPoint = pointsystem[i] || 0;
    posRes[name].killPoint = killPoint;
    i  = 1;
  });
  return Object.values(posRes);
};
console.log(positionAdd(arrayObject));

output getting

[
    { name: 'l2k', win: 0, positionPoint: 3, killPoint: 1 },
    { name: 'l2kb', win: 0, positionPoint: 7, killPoint: 5 },
    { name: 'gd', win: 0, positionPoint: 4, killPoint: 0 }
]

output expected

  [
    { name: "l2k",  win: 1, positionPoint: 12, killPoint: 10 },
    { name: "l2kb", win: 0, positionPoint: 9, killPoint: 6 },
    { name: "gd",   win: 0, positionPoint: 8, killPoint: 0 },
    { name: "l2kb", win: 0, positionPoint: 7, killPoint: 5 },
    { name: "gd",   win: 0, positionPoint: 6, killPoint: 0 },
    { name: "l2k",  win: 0, positionPoint: 5, killPoint: 4 },
    { name: "gd",   win: 0, positionPoint: 4, killPoint: 0 },
    { name: "l2k",  win: 0, positionPoint: 3, killPoint: 1 },
  ]

CodePudding user response:

The reason your function doesn't work is you're creating an object for posRes with the keys being the arrayObject.name

This will only output 3 items in the object since you only have 3 unique names in the array.

I reworked your positionAdd function so it's not using forEach and saving to an object, but instead just creating a new array by using .map() and it gives the expected result:

const positionAdd = (objects) => {
  const posRes = objects.map(({ name, killPoint }, idx) => ({
    name,
    win: winCount[idx] ?? 0,
    positionPoint: pointsystem[idx] ?? 0,
    killPoint,
  }));
  return posRes;
};

CodePudding user response:

Something more like:

let arrayObject = [
  { name: "l2k", killPoint: 10 },
  { name: "l2kb", killPoint: 6 },
  { name: "gd", killPoint: 0 },
  { name: "l2kb", killPoint: 5 },
  { name: "gd", killPoint: 0 },
  { name: "l2k", killPoint: 4 },
  { name: "gd", killPoint: 0 },
  { name: "l2k", killPoint: 1 },
];
let winCount = [1, 0, 0, 0, 0, 0];
let pointsystem = [12, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0];


const positionAdd = (objects) => 
  objects.map((obj, index)=>
    ({...obj, win: winCount[index] || 0, positionPoint: pointsystem[index] || 0})

console.log(positionAdd(arrayObject));

So we map over the array, keeping each element in its position and adding the relevant winCount and pointsystem values as needed, based on the index of the object.

Point of OCD, if you're going to camelCase winCount, be consistent: pointSystem.

  • Related