简体   繁体   English

更改 object 中的值而不更改父级 object

[英]Change values in object without changing parent object

I have a object with some (cards) values and I would like have another object which has values converted (cards by name) by another function.我有一个带有一些(卡片)值的 object,我想要另一个 object,它的值被另一个 function 转换(按名称的卡片)。

cardsHand = [
{ value: "1", suit: "C" },
{ value: "13", suit: "C" },
{ value: "12", suit: "C" },
{ value: "11", suit: "C" },
{ value: "10", suit: "C" },
];

function sortCards(cardsHand) {
//SORT VALUES
let sortedCardsByValue = [];
sortedCardsByValue = cardsHand.sort(
(currCardA, currCardB) => currCardA.value - currCardB.value);

//CHANGE VALUES
let convertedCards = cardsHand;
convertedCards.forEach((element) => {
if (+element.value === 1) element.value = `A`;
if (+element.value === 11) element.value = `J`;
if (+element.value === 12) element.value = `Q`;
if (+element.value === 13) element.value = `K`;
});

return {
convertedCards,
sortedCardsByValue,
};
}

console.log(sortCards(cardsHand).convertedCards);
console.log(sortCards(cardsHand).sortedCardsByValue);

So I would like to achive object sortedCardsByValue:所以我想实现 object sortedCardsByValue:

[
{ value: '1', suit: 'C' },
{ value: '10', suit: 'C' },
{ value: '11', suit: 'C' },
{ value: '12', suit: 'C' },
{ value: '13', suit: 'C' }
]`

and object convertedCards (which is sorted like parent but with changed names for example 1->A; 11->J):和 object convertedCards(排序方式与父级类似,但名称已更改,例如 1->A;11->J):

[
{ value: 'A', suit: 'C' },
{ value: '10', suit: 'C' },
{ value: 'J', suit: 'C' },
{ value: 'Q', suit: 'C' },
{ value: 'K', suit: 'C' }
]

But my code from the beginning creates both objects the same.但是我的代码从一开始就创建了两个相同的对象。

I have made some functions to solve this bug.我做了一些功能来解决这个错误。 I've tried map methos, replace method, forEach, Object.values().我试过 map 方法,替换方法,forEach,Object.values()。

This let convertedCards = cardsHand;let convertedCards = cardsHand; means - create a new variable with a reference to exactly the same object方法 - 创建一个新变量,引用完全相同的 object

This let sortedCardsByValue = [];let sortedCardsByValue = []; does nothing because you assign a different array in the next line什么都不做,因为你在下一行分配了一个不同的数组

Sort works in place so you don't have to assign to a new variable排序就地工作,因此您不必分配给新变量

 const cardsHand = [{ value: "1", suit: "C" }, { value: "13", suit: "C" }, { value: "12", suit: "C" }, { value: "11", suit: "C" }, { value: "10", suit: "C" }, ]; function sortCards(cardsHand) { cardsHand.sort( (currCardA, currCardB) => currCardA.value - currCardB.value); let convertedCards = cardsHand.map(obj => ({...obj })); convertedCards.forEach((element) => { if (+element.value === 1) element.value = `A`; if (+element.value === 11) element.value = `J`; if (+element.value === 12) element.value = `Q`; if (+element.value === 13) element.value = `K`; }); return { convertedCards, sortedCardsByValue: cardsHand, }; } const { convertedCards, sortedCardsByValue, } = sortCards(cardsHand) console.log(convertedCards); console.log(sortedCardsByValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM