迭代对象中的所有属性,查找并替换

时间:2019-05-02 23:52:26

标签: javascript

我有从代码中生成的Javascript对象,播放器对象是

@Component
public class TokenUpdateService {

    @Autowired
    ABCSerivceConnector connector;

    @Async
    public void updateBIToken(List<TokenUpdateRecord> updateRecords) {
        ABCResponse response = connector.updateToken(updateRecords);
        int skippted = response.getSkipped().size();
        int success = response.getSuccess().size();
        int failed = response.getError().size();
    }
}

@Component
public class UpdatePublisherImpl implements UpdatePublisher {

@Autowired
private TokenUpdateService tokenUpdateService;

public void updateNAN(AccountUpdateRecord updateRecord, String batchId) {
        //updateRecords = list of records
        //below line makes Async call.
        tokenUpdateService.updateBIToken(updateRecords);
    }
}

我的问题是如何遍历播放器对象中的所有卡并将所有“ A” faceValue替换为1?使用findIndex还是拼接?

const player = {
   cards: []
}

1 个答案:

答案 0 :(得分:1)

您可以使用Array map卡对象来更新其值

player.cards = player.cards.map(card => {
    if (card.face === 'A') card.faceValue = 1;
    return card;
});

更新: 如果我们不生成新值,最好使用forEach

player.cards.forEach(card => {
    if (card.face === 'A') card.faceValue = 1;
});