【问题标题】:Update value of state when looping through object循环对象时更新状态值
【发布时间】:2020-02-12 05:40:43
【问题描述】:

我有以下具有这些属性的对象:

我需要循环我的代码 sn-p 以便每次循环时变量 arrayPosition 都会增加,file[arrayPosition].file 的值也会根据 arrayPosition 发生变化。 this.state.videoSource 的值也会动态变化,因为它基于 file[arrayPosition].file

这是我现在的代码:

const response = await dataProvider(GET_MANY, 'vid', { ids: videoId })
const file = response.data;
var arrayPosition = 0;

var sigkey = "key";
var formBody = new FormData();
formBody.set('ver', "1.2");
formBody.set('key', "key");
formBody.set('video_id', file[arrayPosition].file);
formBody.set('user_id', "1234");
formBody.set('format', "json");
formBody.set('ip', "");
formBody.set('tts', "0");
formBody.set('nonce', Math.round((new Date()).getTime() / 1000));

var sign_fields = [formBody.get('video_id'), formBody.get('user_id'), formBody.get('ip'), formBody.get('tts'), formBody.get('ver'), formBody.get('key'), formBody.get('nonce')];
var data = sign_fields.join(':');
var signature = hmacsha256(data, sigkey);

formBody.set('sig', signature);

var formBodyStringified = new URLSearchParams(formBody).toString();

 const resJson = await fetch(Config.api.livebox, {
       method: 'POST',
       body: formBodyStringified,
       headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
       }
   }).then(res => res.json());
   const finalJsonUrl = 'https:' + resJson.hls;
   this.setState({ videoSource: finalJsonUrl });
   arrayPosition++;

任何建议我如何循环此代码示例,以便file[arrayPosition].file 的值会根据arrayPosition 的值而改变?

如果有不清楚的地方,请询问。 提前谢谢你。

【问题讨论】:

  • 你想达到什么目的?您想为每个file 获得一个form 吗?你可以把这段代码放在files.forEach ...
  • 我知道我应该使用某种循环,但它们不是我的强项,因此我在这里。是的,我基本上想为每个文件获取一个表单。

标签: javascript reactjs loops object


【解决方案1】:

因此,基于您要为每个 file 创建一个 form 的假设,您可以使用 map 方法尝试类似的操作。 map 方法接受一个数组并根据前一个数组的引用创建一个new array


const response = await dataProvider(GET_MANY, 'vid', { ids: videoId })
const file = response.data;
var arrayPosition = 0;

let formPerFile = file.map( f => {

  var sigkey = "key";
  var formBody = new FormData();
  formBody.set('ver', "1.2");
  formBody.set('key', "key");
  // HEre instead of use the reference of file wth the arrayIndex we just can use the f variable provided by our iterator 
  formBody.set('video_id', f.file);
  formBody.set('user_id', "1234");
  formBody.set('format', "json");
  formBody.set('ip', "");
  formBody.set('tts', "0");
  formBody.set('nonce', Math.round((new Date()).getTime() / 1000));

  var sign_fields = [formBody.get('video_id'), formBody.get('user_id'),  formBody.get('ip'), formBody.get('tts'), formBody.get('ver'), formBody.get('key'), formBody.get('nonce')];
  var data = sign_fields.join(':');
  var signature = hmacsha256(data, sigkey);

  formBody.set('sig', signature);

  var formBodyStringified = new URLSearchParams(formBody).toString();

// YOu need to make this request per file? 
// Or you have a service taht can `post` all the forms from one request?
  const resJson = await fetch(Config.api.livebox, {
       method: 'POST',
       body: formBodyStringified,
       headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
       }
   }).then(res => res.json());

   return 'https:' + resJson.hls;

} );

  // Here you can store the video sources returned from the map method in your state instead of storing just one value
   this.setState({ videoSource: formsPerFile });

这种方法是否适合您的目标?还是你在尝试不同的东西?

【讨论】:

  • 这很好,但是该值现在位于键 [[PromiseValue]] 下,这并不完全有用。我可以以某种方式修改它看起来像这样吗? 0: value
  • 这看起来像其他问题,我记得,map 方法不是作为异步方法构建的,也许this post 将帮助您解决其他问题。
猜你喜欢
  • 2018-03-20
  • 1970-01-01
  • 2021-11-10
  • 2023-03-22
  • 1970-01-01
  • 2016-10-06
  • 2020-07-01
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多