更新数组中对象的嵌套属性

huangapple go评论91阅读模式
英文:

Update nested property of object in an array

问题

我有一个类似这样的对象:

data: {
  users: [
    {
      id: "someString",
      liquidityPositions: [
        {
          balance: "someString",
          pair: {
            id: "someString",
            reserve0: "someString",
            reserve1: "someString",
            token0: {
                      id: "someString"
            },
            token1: {
                      id: "someString"
            },
          }
        },

        ...
      ]
    }
  ]
}

我试图修改token0token1的属性,以包括额外的属性symbol

let updated = myObj.data.users[0].liquidityPositions.map(
    (liquidityPosition) => ({
        ...liquidityPosition,
        token0: {
            ...liquidityPosition.token0,
            symbol: "yourSymbolForToken0"
        },
        token1: {
            ...liquidityPosition.token1,
            symbol: "yourSymbolForToken1"
        }
    })
);

如何在保留其他所有内容的情况下向token0token1添加新属性?

英文:

I have an object that looks like this:

data: {
  users: [
    {
      id: "someString",
      liquidityPositions: [
        {
          balance: "someString",
          pair: {
            id: "someString",
            reserve0: "someString",
            reserve1: "someString",
            token0: {
                      id: "someString"
            },
            token1: {
                      id: "someString"
            },
          }
        },

        ...
      ]
    }
  ]
}

I'm trying to change the property of token0 and token1 to include an extra property symbol:

let updated = myObj.data.user[0].liquidityPositions.map(
    (liquidityPosition) => ({
        ...balance,
        //I'm not sure how to update here

How can I add a new property to token0 and token1 while preserving everything else?

答案1

得分: 2

我会这样做。

data.users[0].liquidityPositions.map(({pair, ...rest}) => {
  const token0 = {...pair.token0, symbol: 'asdf'};
  const token1 = {...pair.token1, symbol: 'qwerty'};
  return {...rest, pair: {...pair, token0, token1}};
});
英文:

I'd do it like this.

data.users[0].liquidityPositions.map(({pair, ...rest}) => {
  const token0 = {...pair.token0, symbol: 'asdf'};
  const token1 = {...pair.token1, symbol: 'qwerty'};
  return {...rest, pair: {...pair, token0, token1}};
});

答案2

得分: 1

这不太简洁,但似乎可以工作

let updated = data.users[0].liquidityPositions.map((liquidityPosition) => {
    let oldPair = liquidityPosition.pair;
    return {
        ...liquidityPosition,
        pair: {
            ...oldPair,
            token0: {
                ...oldPair.token0,
                id: "newString"
            },
            token1: {
                ...oldPair.token1,
                id: "newString"
            }
        }
    };
});

你也可以选择内联旧引用,如果你愿意

let updated = data.users[0].liquidityPositions.map((liquidityPosition) => ({
    ...liquidityPosition,
    pair: {
        ...liquidityPosition.pair,
        token0: {
            ...liquidityPosition.pair.token0,
            id: "newString"
        },
        token1: {
            ...liquidityPosition.pair.token1,
            id: "newString"
        }
    }
}));
英文:

This is not so concise, but it seems to work

let updated = data.users[0].liquidityPositions.map((liquidityPosition) => {
	let oldPair = liquidityPosition.pair
	return {
		...liquidityPosition,
		pair: {
			...oldPair,
			token0: {
				...oldPair.token0,
				id: "newString"
			},
			token1: {
				...oldPair.token1,
				id: "newString"
			}
		}
	}
})

Also you can just inline the old reference, if you want

let updated = data.users[0].liquidityPositions.map((liquidityPosition) => ({
	...liquidityPosition,
	pair: {
		...liquidityPosition.pair,
		token0: {
			...liquidityPosition.pair.token0,
			id: "newString"
		},
		token1: {
			...liquidityPosition.pair.token1,
			id: "newString"
		}
	}
}))

huangapple
  • 本文由 发表于 2023年2月27日 16:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定