Javascript:显示数组结果的正确方法

时间:2018-05-01 10:05:12

标签: javascript arrays ecmascript-6

我有一个数组来定义我的变量:

const nbActions = 13;
const sdgValues = ['1','2', '5', '6', '3','0','2','0','0','6','0','0','0','1','0','0','0'];

let sdg = [];
for (let i = 0; i < sdgValues.length; ++i) {
    sdg[i] = (sdgValues[i] / nbActions * 100).toPrecision(4);       
}

但是现在,我想在dataSDGs上显示这个值,而不是通过骨骼定义所有sdg [x]:

const dataSDGs = {
   data: [
            sdg['0'],
            sdg['1'],
            sdg['2'],
            sdg['3'],
            ...
    ]
}

我的目标是获取此值以使用chart.js制作图表

2 个答案:

答案 0 :(得分:0)

    const nbActions = 13;
const sdgValues = ['1','2', '5', '6', '3','0','2','0','0','6','0','0','0','1','0','0','0'];


let sdg =sdgValues.map((item)=>{
    return (item / nbActions * 100).toPrecision(4);   
})

var dataSDGs = {};
dataSDGs.data = sdg;
console.log(dataSDGs);

答案 1 :(得分:0)

以下应该这样做,不需要循环:

&#13;
&#13;
public class Loggedi extends AppCompatActivity {

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private FirebaseAuth mAuth;
    private String mVerificationId;
    private PhoneAuthProvider.ForceResendingToken mResendToken;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loggedi);


        final Button sendbtn;
        TextView textView;
        final EditText mobile, verification;




        sendbtn = findViewById(R.id.sendbtn);
        textView = findViewById(R.id.textView);
        mobile = findViewById(R.id.mobile);
        mAuth = FirebaseAuth.getInstance();
        verification = findViewById(R.id.verification);


        sendbtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 mobile.setEnabled(false);
                sendbtn.setEnabled(false);

                String phone;
                phone = mobile.getText().toString();
                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        phone,        // Phone number to verify
                        60,                 // Timeout duration
                        TimeUnit.SECONDS,   // Unit of timeout
                        Loggedi.this,
                        mCallbacks);

                String verificationcode  = verification.getText().toString();
                PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, verificationcode);
                signInWithPhoneAuthCredential(credential);

            }
        });

        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
            signInWithPhoneAuthCredential(phoneAuthCredential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                Toast.makeText(Loggedi.this, "Invalid Verification", Toast.LENGTH_SHORT).show();

            }
            @Override
            public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) {

                mVerificationId = verificationId;
                mResendToken = token;
                sendbtn.setText("Verify Code");
            }
        };

        };

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task <AuthResult> task) {
                        if (task.isSuccessful()) {
                            Intent welcome = new Intent(Loggedi.this, welcomelayout.class);
                            startActivity(welcome);
                            finish();

                            FirebaseUser user = task.getResult().getUser();
                            // ...
                        } else {


                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                Toast.makeText(Loggedi.this, "Invalid Verification Code", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
    }
}
&#13;
&#13;
&#13;