Итак, я пытаюсь присвоить объект свойству объекта.Я нашел легкий успех, но объект был назначен неправильно.Я сопоставляю массив, чтобы показать карты.
Есть большие карты и маленькие карты.Большим картам может быть назначено заранее определенное количество и тип маленьких карт.Вы можете иметь несколько одинаковых больших карт в колоде.Большие карты хранятся в массиве
Мои большие карты имеют свойство объекта:
upgrades: {
preterminedSmallCard: null,
preterminedSmallCard: null
}
Я пытаюсь добавить клик, основываясь на уникальном идентификаторе каждой Большой карты вмассив, маленькая карта для слота.
Вместо того, чтобы добавлять его к одному экземпляру в массиве, он добавляет его к каждой большой карте с тем же именем.Если это другое имя, оно не добавляет его.
Вот мой код с заостренными комментариями:
addUpgradeHandler = (card) => {
//determines which upgrade type to add - console.logging properly
const upgradeType = this.props.match.params.type;
//finding the index of targeted ship
const shipIndex = this.props.shipInfo.findIndex(index => {
/**
* Router passes the id of the targeted ship, the compared against
* returns true to the targeted ship, false for the ones not targeted
* ID's generated with npm package uniqID - unique id for each instance in array
**/
return (index.id === this.props.match.params.id);
});
console.log(shipIndex); //evaluates properly to the target index position
//mapping the state to a copy
const newShips = [...this.props.shipInfo];
//targeting the upgrades property inside of the correct index pos
const upgrades = newShips[shipIndex].upgrades;
console.log(upgrades); //returns only one index pos of the state with the upgrade equipped
const currentPoints = this.props.points;
//checking if adding the card will bust the max points allowed
if (currentPoints + card.points < 400) {
console.log(upgrades[upgradeType]); // returns one object, the value stored in the proper place
/**
* Issue starts here
* - card -> represents object that the map below is, issue persists if object is explicitly written
* assigns upgrade card object to the correct property type, but for every ship with the same name
* The name of the ship is not being evaluated anywhere in this function
**/
//not intended
upgrades[upgradeType] = card;
}
}
Другие странные вещи:
- Iудалите большую карту из массива, сращивая, используя то же сравнение, что и выше, и только сращивая один, желаемым способом.
- После сращивания из массива, если я добавлю большую карту с тем же именем,он отображается с теми же маленькими картами, которые все еще подключены, даже если у него совершенно новый идентификатор с использованием
uniqId()
Итак, TL; DR, мои методы нацеливания на определенный индекс - это запись в консоль должным образом,но объект назначается неправильно, по имени, которое я вообще не оцениваю в своем коде.