Почему моя переменная не работает при принятии в качестве аргумента?Javascript

Форум по Javascript
Ответить
Anonymous
 Почему моя переменная не работает при принятии в качестве аргумента?

Сообщение Anonymous »

Для человека, который пропускает его, я пытаюсь добавить год смерти, используя метод уменьшения для массива объектов. < /p>
Например: < /p>
{
name: "Carly",
yearOfBirth: 2018,
},
< /code>
{
name: 'Carly',
yearOfBirth: 2018,
yearOfDeath: 2025
}
< /code>
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)
}
}, {})< /code>




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)
}
}, {})< /code>




Подробнее здесь: https://stackoverflow.com/questions/795 ... n-argument
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»