【问题标题】:Changing a value of an index in a buffer array更改缓冲区数组中的索引值
【发布时间】:2022-01-19 02:17:30
【问题描述】:

所以我使用 requestly 来修改 http 响应

function modifyResponse(args) {
  const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args;
  console.log(response)
  return response;
}

所以这个函数返回响应,响应是来自服务器的实际响应,我可以用函数改变它,问题是:当我 console.log 得到这个响应时

ArrayBuffer(53)
byteLength: 53
[[Prototype]]: ArrayBuffer
[[Int8Array]]: Int8Array(53)
[[Uint8Array]]: Uint8Array(53)
[[ArrayBufferByteLength]]: 53
[[ArrayBufferData]]: 629

https://ibb.co/zQVbvLq

我尝试读取数组(上图),发现都是ascii,这不是问题,我想做这样的事情

Int8Array[0] = 10

问题是我没有这方面的知识,不知道如何通过 modifyResponse 函数访问 Int8Array。

我必须把函数改成这样:

function modifyResponse(args) {
  const {method, url, response, responseType, requestHeaders, requestData, responseJSON} = args;
  //console.log(response)
response[Int8Array[0]] = 10
  return response;
}

我基本上必须访问响应,然后访问 Int8Array,然后更改索引 0 处的元素,但它不起作用:/ 谁能帮帮我?

【问题讨论】:

    标签: javascript arrays integer buffer requestly


    【解决方案1】:

    [[Int8Array]] 是一个内部属性 - 您不能像访问任何其他属性一样简单地访问它。

    如果您想将ArrayBuffer 编辑为Int8Array,您需要像这样构造它:

    const array = new Int8Array(response);
    array[0] = 10;
    return response;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 2021-01-11
      • 2020-05-05
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多