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
|
2 Answers
Reset to default 3There's a few problems here.
accum
is supposed to be an array of elements. But you reset it to an empty array when do you this:accum = []
.
- Incorrect use of
=
operator inaccum.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
yod
is an alias ofyearOfDeath
, which frees up theyearOfDeath
variable within themap
callback- If
yod
is defined, then that is assigned toyearOfDeath
, otherwise the current year is. - In your code
accum.push()
inaccum.push(person.yearOfDeath = currentYear)
andaccum.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 beperson.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
yearOfDeath
to 2025? You really don't needreduce()
, something simple likeforEach()
would work. – zer00ne Commented Mar 13 at 22:07