Home > other >  js adding a new element in array
js adding a new element in array

Time:09-01

i am working on node js app, there i am facing a problem that i have to add position points in array but every position point is different like 1 st array/object need to have 10 then next array/object have to be 7

let converter = [
  { name: 'team-1', killPoint: 8 },
  { name: 'team-2', killPoint: 7 },
  { name: 'team-7', killPoint: 56 },
  { name: 'team-9', killPoint: 68 }
]
const pointsystem = [10, 7, 6, 5, 4, 3, 2, 1, 0];
const positionAdd = (objects) => {
  let i = 0
  const posRes = {};
  objects.forEach(({
    name,
    killPoint
  }) => {
    posRes[name] = posRes[name] || {
      name,
      positionPoint: 0,
      killPoint: 0,
    };
    posRes[name].positionPoint = pointsystem[i 1];
    posRes[name].killPoint = killPoint;
  });
  return Object.values(posRes);
};
console.log(positionAdd(converter));

trying with this code but its not working as wanting

output wanting from it

[
  { name: 'team-1', positionPoint: 10, killPoint: 8 },
  { name: 'team-2', positionPoint: 7, killPoint: 7 },
  { name: 'team-7', positionPoint: 6, killPoint: 56 },
  { name: 'team-9', positionPoint: 5, killPoint: 68 }
]

if have more team's then point should going on until 0....

CodePudding user response:

Use map() instead of appending to the result array.

Use the second argument to the callback function to get the array index, and use that to index into pointsystem.

Use object spread syntax to merge that into the objects from converter.

let converter = [
  { name: 'team-1', killPoint: 8 },
  { name: 'team-2', killPoint: 7 },
  { name: 'team-7', killPoint: 56 },
  { name: 'team-9', killPoint: 68 }
]
const pointsystem = [10, 7, 6, 5, 4, 3, 2, 1, 0];
const positionAdd = (objects) => objects.map((obj, i) => ({...obj, positionPoint: pointsystem[i]}));

console.log(positionAdd(converter));

CodePudding user response:

The following will also change the original objects. I am not sure what OP's intention is:

const data = [
  { name: 'team-1', killPoint: 8 },
  { name: 'team-2', killPoint: 7 },
  { name: 'team-7', killPoint: 56 },
  { name: 'team-9', killPoint: 68 }
],points = [10,7,6,5,4,3,2,1,0];
console.log(data.map((d,i)=>{
 d.positionPoint=points[i]??0;
 return d;
}));

CodePudding user response:

You are adding 1 to your variable i, but not implementing i after each loop. You need to increment i by 1 after each iteration.

Example:

posRes[name].positionPoint = pointsystem[i];
i  = 1;

Test:

let converter = [
  { name: 'team-1', killPoint: 8 },
  { name: 'team-2', killPoint: 7 },
  { name: 'team-7', killPoint: 56 },
  { name: 'team-9', killPoint: 68 }
]
const pointsystem = [10, 7, 6, 5, 4, 3, 2, 1, 0];
const positionAdd = (objects) => {
  let i = 0
  const posRes = {};
  objects.forEach(({
    name,
    killPoint
  }) => {
    posRes[name] = posRes[name] || {
      name,
      positionPoint: 0,
      killPoint: 0,
    };
    posRes[name].positionPoint = pointsystem[i];
    i  = 1;
    posRes[name].killPoint = killPoint;
  });
  return Object.values(posRes);
};
console.log(positionAdd(converter));

  • Related