【问题标题】:JavaScript nested array not able to use values?JavaScript 嵌套数组不能使用值?
【发布时间】:2021-12-03 15:14:25
【问题描述】:

我正在为游戏开发 Telegram 程序。 Step1:我必须调用MYSQL来拉取lua中的数据。步骤 2:我必须在我的 telegram.js 文件中将表数据发送到我的 NUI。 Step3:我必须将字符串转换为数组。

我在我的 telegram.js 中有这个,这是被提取数据的确切布局。

  var Atest = [
    (a1 = [7, 1, 'This is a test', 1, '05:30:35pm', '09/23/2021', 1]),
    (b1 = [7, 1, 'Test Test', 1, '05:40:35pm', '09/26/2021', 2]),
  ];

这可以正常工作,因为它在 .js 文件中。当我从我的 telegram.lua 文件中带入一个字符串时,我可以将数据拆分到数组 console.log

for (const [key, value] of Object.entries(Atest)) {
      console.log(`${key}: ${value}`);
    }
0: 7, 1, This is a test, 1, 05:30:35pm, 09/23/2021, 1
1: "diff string ect..."

我遇到的一个问题是内部值不会注册。它同样适用于这个示例,因为它在我的 .js 中,但是当带来一个字符串时,我无法让 js 让内部数组拥有索引。

在按顺序发布时使用for k,v in pair do 也不准确,如果我可以将字符串转换为完整的数组也没关系。

我的菜单程序中需要这个的代码如下。

telegram.js

function newMessages(nTT) {
    for (const [key, value] of Object.entries(nTT)) {
      console.log(`${key}: ${value}`);
    }
    sleep(100);
    console.log(nTT);
    for (const [k, v] of nTT.entries()) {
      var split_date = v[5].split('/');
      var newb = document.createElement('a');
      var newb_i = document.createElement('input');
      newb_i.setAttribute('id', `checkbox_${k + 1}`);
      newb_i.setAttribute('class', 'checkbox');
      newb_i.setAttribute('type', 'checkbox');
      newb_i.setAttribute('value', 'value');
      newb_i.setAttribute('data-delete_tele_number', v[6]);
      newb.setAttribute('id', 'new_b');
      newb.setAttribute('data-id', v[0]);
      newb.setAttribute('data-new_old', v[1]);
      newb.setAttribute('data-message', v[2]);
      newb.setAttribute('data-to_id', v[3]);
      newb.setAttribute('data-time', v[4]);
      newb.setAttribute('data-date', v[5]);
      newb.setAttribute('data-tele_number', v[6]);
      newb.setAttribute('onclick', 'reply_click(this)');
      newb.innerHTML = `From ${v[0]} on ${split_date[0]}/${split_date[1]} # ${v[6]}`;
      document.getElementById('new_menu').appendChild(newb).prepend(newb_i);
    }
    check_checked();
    disable_tele_input();
  }

我将不得不最终更改我的值数据结构,因为我使用 for k, v 循环的方式将我的数据库中的“id”使用 v[id] 之类的东西而不是调用在本例中为 v[0]。这很容易,并且已经在从 lua 发送的字符串中,但是如果我可以让字符串注册为具有“键”和“值”的整个数组,那么我刚刚构建的所有内容都将无法正常工作,哈哈。

我花了很多时间试图找到一种将字符串带入 js 并重建它的方法,使其成为一个 ARRAY。任何帮助将不胜感激。我试图将 arr.push 到每个值的新数组,我有 split() 基于添加特殊的“-”来解构和重建。我已经使用 RegExp 进行了匹配。

这是我数据库中的实际数据。

MSYQL DB

INSERT INTO `telegram` (`id`, `new_old`, `message`, `to_id`, `time`, `date`, `tele_number`) VALUES
(7, 1, 'This is a test Message. Testing 123.', 1, '05:30:35PM', '09/23/2021', 1),
(5, 1, 'Test. For science.', 7, '05:30:35PM', '09/23/2021', 2),
(7, 0, 'herro', 7, '05:30:35PM', '09/23/2021', 3),
(7, 1, 'Hey', 4, '08:13:17PM', '09/23/2021', 4),
(7, 0, 'Bro', 7, '08:13:17PM', '09/23/2021', 5),
(7, 1, 'Brommblkdljf', 7, '08:13:17PM', '09/23/2021', 6),
(7, 1, 'test', 4, '08:13:17PM', '09/23/2021', 7),
(7, 1, 'bro this is a new messate', 1, '08:13:17PM', '09/23/2021', 8),
(7, 1, 'alskdjflsddlfkj', 7, '08:13:17PM', '09/23/2021', 9),
(7, 1, 'lets see if the whole thing works', 7, '04:56:27PM', '10/04/2021', 10),
(7, 1, 'bro lets try this again', 7, '05:05:23PM', '10/04/2021', 11);

这是来自 lua 的排序

telegram_sv.lua

for k, v in ipairs(nTele) do
  print(k, json.encode(v))
  n[#n+1] = v
end
  newTele = json.encode(n)
  print(newTele)
telegram.js

 function n_string(nT) {
    nTS = nT.replace("[", "");
    nTS = nTS.replace("]", "");
    nTs = nTS.replace(",",", ")
    let regexp = /\{([^}]*)\}/g;
   nTT = nTS.match(regexp)
    for (const [key, value] of Object.entries(nTT)) {
      console.log(`${key}: ${value}`);
    }
    console.log(nTT[0]);
  }
...tons more code under... sends to newMessages(nTT) at some point...

谢谢大家!

¯\_(ツ)_/¯

【问题讨论】:

    标签: javascript arrays string lua


    【解决方案1】:

    所以我已经发现了我的错误。我忘了 JSON.parse()。

    newTT = [];
       for ([k, v] of Object.entries(nTT)) {
        newTT.push(JSON.parse(v))
       }
    

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 2022-11-28
      • 2019-11-25
      • 2017-09-01
      • 2018-10-14
      相关资源
      最近更新 更多