2 回答

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以結(jié)合使用filter、map和join來使其正常工作。
示例不是 React,但向您展示了如何實(shí)現(xiàn)。
const fruits = [
? {
? ? source: "Apple",
? ? code: 101,
? },
? {
? ? source: "banana",
? ? code: 105,
? },
? { source: "Mango",
? ? code: 107,
? },
];
const state = [101, 105];
const getFruitNames = () => fruits
? .filter(({ code }) => state.includes(code)) // Get all fruit objects where the code is also in state
? .map(({ source }) => source) // Only return the source (name)
? .join(", "); // Turn into a comma seperated string
console.log(`My Fruits: ${getFruitNames()}`);

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以結(jié)合使用 filter 和 map 方法,
this.props.fruits.filter((fruit)?=>?this.state.myfruits.includes(fruit.code)) ??????????.map((fruit)?=>?fruit.source)
添加回答
舉報(bào)