在React JSON API中调用Array中的对象调用

时间:2018-03-08 19:58:14

标签: arrays json reactjs object iteration

我正在进行一次API调用,在React应用程序中返回JSON数据。数据包含嵌套在数组中的对象。我想访问对象中的数据。

我能够访问并遍历数组数据并使用以下内容进行记录:

loadSearchResults = (event) => {
event.preventDefault();
var search = this.state.value;

axios.get(`https://images-api.nasa.gov/search?q=` + `search`)
  .then(res => {
    const results = res.data.collection.items;
    console.log(results);
  });

}

注销数组。 enter image description here

我想访问每个对象内的数据。 enter image description here

如何迭代并显示对象数据(即描述,中心,媒体类型)?换句话说,在遍历数组之后,我如何遍历数组中的对象?

1 个答案:

答案 0 :(得分:0)

Object.values()应该有效:

public void rbClick(View view) {

    final boolean check = ((RadioButton) view).isChecked();

    int selectedTime = 0;


    switch (view.getId()) {
        case R.id.radioButton5min:
            if (check) {

                selectedTime = 300;

                stopTimer();
            }
            break;

        case R.id.radioButton10min:

            if (check) {

                selectedTime = 600;

                stopTimer();
            }
            break;


        case R.id.radioButton15min:

            if (check) {

                selectedTime = 900;

                stopTimer();
            }
            break;

        case R.id.radioButton20min:

            if (check) {
                selectedTime = 1200;

                stopTimer();

            }
            break;


        case R.id.radioButton30min:

            if (check) {


                selectedTime = 1800;

                stopTimer();

            }
            break;


        case R.id.radioButton45min:

            if (check) {

                selectedTime = 2700;

                stopTimer();
            }
            break;


        case R.id.radioButton60min:

            if (check) {

                selectedTime = 3600;

                stopTimer();
            }
            break;

        case R.id.radioButton90min:

            if (check) {

                selectedTime = 5400;

                stopTimer();
            }
            break;


        case R.id.radioButton2H:

            if (check) {

                selectedTime = 7200;

                stopTimer();

            }
            break;


        case R.id.radioButton4H:

            if (check) {

                selectedTime = 14400;

                stopTimer();
            }
            break;


        case R.id.radioButton8H:

            if (check) {

                selectedTime = 28800;

                stopTimer();
            }
            break;


    }


    countDownTimer = new CountDownTimer(selectedTime * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
             seconds = millisUntilFinished;


            textView.setText(String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(seconds),
                    TimeUnit.MILLISECONDS.toMinutes(seconds) - TimeUnit.HOURS.toMinutes
                            (TimeUnit.MILLISECONDS.toHours(seconds)), TimeUnit.MILLISECONDS.toSeconds(seconds)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(seconds))));


        }

        @Override
        public void onFinish() {

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(SetTime.this, AlertReciver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(SetTime.this, 1, intent, 0);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }

            countDownTimer = null;
        }

    }.start();


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {


            Intent intent = new Intent(SetTime.this, ParkingScreen.class);
            Bundle timeTransfer = new Bundle();
            String myAddress = gpsAddress.getText().toString();
            intent.putExtra("myAddress", myAddress);
            timeTransfer.putLong("myTime",seconds);
            intent.putExtras(timeTransfer);
            intent.putExtra("date", date);
            intent.putExtra("time", tvTime.getText().toString());
            intent.putExtra("date", textViewDate.getText().toString());
            startActivity(intent);
        }
    }, 200);
}

public void stopTimer() {

    if (countDownTimer != null) {

        countDownTimer.cancel();
    }
}

public void cbRemindertime(View view) { boolean check = ((CheckBox) view).isChecked(); int counter = 0; switch (view.getId()) { case R.id.cb2min: if (check) { counter = 120; } break; case R.id.cb3min: if (check) { counter = 180; } break; case R.id.cb5min: if (check) { counter = 300; } break; case R.id.cb10min: if (check) { counter = 600; } break; case R.id.cb15min: if (check) { counter = 9000; } break; case R.id.cb20min: if (check) { counter = 1200; } break; case R.id.cb30min: if (check) { counter = 1800; } break; } countDownTimer = new CountDownTimer(counter * 1000, 1000) { @Override public void onTick(long millisUntilFinished) { time = millisUntilFinished; } @Override public void onFinish() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(ReminderTimes.this, AlertReciver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(ReminderTimes.this, 1, intent, 0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } } }; } 内呈现数据(假设此组件具有state /是一个类):

  axios
  .get(`https://images-api.nasa.gov/search?q=` + `search`)
  .then(res => res.data.collection.items)
  .then(json => {
    json.map(obj => console.log(Object.values(obj)))
  })

棘手的部分是你在承诺中,这是异步的,但这是另一个问题