JavaScript使用内联解决方案生成字符串数组

时间:2017-10-14 09:44:10

标签: javascript arrays string

有简单的代码:

public class AuthoritiesFilter extends GenericFilterBean {
        public static final String EMAIL = "email";
        public static final String NAME = "name";
        public static final String GIVEN_NAME = "given_name";
        public static final String FAMILY_NAME = "family_name";
        public static final String PICTURE = "picture";
        public static final String GENDER = "gender";
        public static final String LOCALE = "locale";

        private UserRepository userRepository;

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;

            if (oAuth2Authentication != null && oAuth2Authentication.getUserAuthentication().getDetails() != null) {
                SecurityContextHolder.getContext().setAuthentication(processAuthentication(authentication));
            }

            chain.doFilter(request, response);
        }

        private OAuth2Authentication processAuthentication(Authentication authentication) {
            OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
            Map<String, String> details = (Map<String, String>) oAuth2Authentication.getUserAuthentication().getDetails();

            User user = userRepository.getByEmail(details.get(EMAIL))
                    .orElse(new User());
            updateUser(user, details);
            userRepository.save(user);

            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
                    oAuth2Authentication.getPrincipal(),
                    oAuth2Authentication.getCredentials(),
                    user.getAuthorities().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
            oAuth2Authentication = new OAuth2Authentication(oAuth2Authentication.getOAuth2Request(), token);
            oAuth2Authentication.setDetails(details);
            return oAuth2Authentication;
        }

        private void updateUser(User user, Map<String, String> details) {
            user.setEmail(details.get(EMAIL));
            user.setName(details.get(NAME));
            user.setGivenName(details.get(GIVEN_NAME));
            user.setFamilyName(details.get(FAMILY_NAME));
            user.setPicture(details.get(PICTURE));
            user.setGender(details.get(GENDER));
            user.setLocale(details.get(LOCALE));
        }

        public void setUserRepository(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    }

我想知道是否有一个单行解决方案,我输入字符串和最大值并获得生成行的数组。

3 个答案:

答案 0 :(得分:4)

尝试这一点,如果输入为5,则可以将其设为N并从用户

获取输入

<强>样本

&#13;
&#13;
var result = Array.from(new Array(5),(val,index)=> "some string " + index );
console.log(result);
&#13;
&#13;
&#13;

答案 1 :(得分:2)

&#13;
&#13;
const newArray = [...Array(10)].map((_, i) => `some string with iterating value ${i}`)
console.log(newArray)
&#13;
&#13;
&#13;

您可以使用扩展运算符并创建一个所需长度的新数组,循环(map)并返回该字符串。这将创建一个长度为(10)的新数组,其中包含您想要的字符串。

答案 2 :(得分:1)

如何使其成为可重复使用的功能?例如。

// Replaces '{i}' with the index number
var generateStringArray = (length, string) => Array.from(new Array(length), (val, index) => string.replace('{i}', index))

console.log(generateStringArray(6, "some string {i}"));