javascript - Why is my variable not working when passed as an argument? - Stack Overflow

For the person who is missing it, I am trying to add the year of death by using the reduce method for a

For the person who is missing it, I am trying to add the year of death by using the reduce method for an array of objects.

For example:

{
name: "Carly",
yearOfBirth: 2018,
},
{
name: 'Carly',
yearOfBirth: 2018,
yearOfDeath: 2025
}

Why is using the passed variable causing the code to not function properly? I am new to this.

accum.push(passed = currentYear)

accum.push(person.yearOfDeath = currentYear)

See compared live examples with and without use of the variable.

const people = [
      {
        name: "Carly",
        yearOfBirth: 2018,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
    ]
    
    const newArray = people.reduce((accum, person) => {
      accum = []
        accum.push(person)
      
      let passed = person.yearOfDeath
      const currentYear = new Date().getFullYear();
      if (!passed) {
        accum.push(passed = currentYear)

console.log(accum)
      }
}, {})

For the person who is missing it, I am trying to add the year of death by using the reduce method for an array of objects.

For example:

{
name: "Carly",
yearOfBirth: 2018,
},
{
name: 'Carly',
yearOfBirth: 2018,
yearOfDeath: 2025
}

Why is using the passed variable causing the code to not function properly? I am new to this.

accum.push(passed = currentYear)

accum.push(person.yearOfDeath = currentYear)

See compared live examples with and without use of the variable.

const people = [
      {
        name: "Carly",
        yearOfBirth: 2018,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
    ]
    
    const newArray = people.reduce((accum, person) => {
      accum = []
        accum.push(person)
      
      let passed = person.yearOfDeath
      const currentYear = new Date().getFullYear();
      if (!passed) {
        accum.push(passed = currentYear)

console.log(accum)
      }
}, {})

const people = [
      {
        name: "Carly",
        yearOfBirth: 2018,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
    ]
    
    const newArray = people.reduce((accum, person) => {
      accum = []
        accum.push(person)
      
      const passed = person.yearOfDeath
      const currentYear = new Date().getFullYear();
      if (!passed) {
        accum.push(person.yearOfDeath = currentYear)

console.log(accum)
      }
}, {})

Share Improve this question edited Mar 13 at 22:07 lunarchild asked Mar 13 at 21:55 lunarchildlunarchild 236 bronze badges 3
  • 3 Why are you trying to change the yearOfDeath to 2025? You really don't need reduce(), something simple like forEach() would work. – zer00ne Commented Mar 13 at 22:07
  • 1 reduce() is used to modify data. In this case it looks like you are not trying to do that. – dram95 Commented Mar 13 at 22:13
  • I am trying to add the year of death by using the reduce method - so is your requirement "to use the reduce method"? Or is it actually to "add the year of death" and, for some reason, you decided "reduce" was the way to go? (ie an XY Problem) – fdomn-m Commented Mar 14 at 9:39
Add a comment  | 

2 Answers 2

Reset to default 3

There's a few problems here.

  1. accum is supposed to be an array of elements. But you reset it to an empty array when do you this: accum = [].
  1. Incorrect use of = operator in accum.push() expects an item to be pushed into the array. You don't need the = operator.

Here is the updated code:

const people = [
    { name: "Carly", yearOfBirth: 2018 },
    { name: "Ray", yearOfBirth: 1962, yearOfDeath: 2011 },
    { name: "Jane", yearOfBirth: 1912, yearOfDeath: 1941 },
  ];
  
  const currentYear = new Date().getFullYear();
  
  const newArray = people.reduce((accum, person) => {
    // Create a copy of the person object to avoid modifying the original
    const updatedPerson = { ...person };
  
    
    if (!updatedPerson.yearOfDeath) {
      updatedPerson.yearOfDeath = currentYear;
    }
  
    accum.push(updatedPerson); // Push the updated object into the array
    return accum;
  }, []);
  
  console.log(newArray);
  

If your goal is simply to add yearOfDeath property to the elements of the array (objects) that do not have it, Array#map would be the ideal method, not Array#reduce.

Here's one way to do it:

const people = [
      {
        name: "Carly",
        yearOfBirth: 2018,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
      {
        name: "Notme",
        yearOfBirth: 2050
      }
];
    
const newArray = people.map(({
        name,
        yearOfBirth,
        yearOfDeath:yod
    }) => {
      const yearOfDeath = yod || new Date().getFullYear();
      
      return {
          name, 
          yearOfBirth, 
          yearOfDeath
      };
});

console.log( newArray );

NOTES

  1. yod is an alias of yearOfDeath, which frees up the yearOfDeath variable within the map callback
  2. If yod is defined, then that is assigned to yearOfDeath, otherwise the current year is.
  3. In your code accum.push() in accum.push(person.yearOfDeath = currentYear) and accum.push(passed) is not serving any legitimate purpose. Since you're using .reduce() as a loop - try .forEath(), as shown below - you just need to assign a value to the desired property, and that would do it. In your case that would be person.yearOfDeath = currentYear;

USING THE forEach ARRAY METHOD

const people = [
      {
        name: "Carly",
        yearOfBirth: 2018,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
      {
        name: "Notme",
        yearOfBirth: 2050
      }
];
    
const newArray = []; 

people.forEach(({
        name,
        yearOfBirth,
        yearOfDeath:yod
    }) => {
      const yearOfDeath = yod || new Date().getFullYear();
      
      newArray.push({
          name, 
          yearOfBirth, 
          yearOfDeath
      });
});

console.log( newArray );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744683267a4587760.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信