gpt4 book ai didi

javascript - 将第一个值设置为 Javascript 数组中的键

转载 作者:行者123 更新时间:2023-12-01 01:58:44 25 4
gpt4 key购买 nike

根据选定的数据表行创建数组

$('#savenlp').click(recordjourney);

function recordjourney() {
var data = table.rows(['.selected']).data().toArray();
console.log( (data) );
console.log( JSON.stringify(data) );
}

数据返回

0 : (8) ["Which", "TitleCase", "QuestionWord", "", "", "", "", ""]

JSON.stringify(data) 返回

[["baseball","Noun","Singular","","","","",""]]

此信息是动态生成的,因此我只想获取第一个值(在本例中为棒球)并将其转换为类似的内容

  "baseball": [
"Noun",
"Singular"
]

我可以返回第一个值(我想要使用的键)

alert(data[0][0]);

我更擅长 PHP,但我正在越来越多地学习 javascript/jquery。

据我了解,javascript 没有关联数组,所以我对如何生成它有点困惑。

最佳答案

const data = [
["baseball","Noun","Singular","","","","",""],
["baseballs","Noun","","Plural","","","","",]
];
const mappedData = data.reduce((acc, row) => { acc[row.shift()] = row.filter(d => d !== ''); return acc; }, {});
console.log(mappedData);

关于javascript - 将第一个值设置为 Javascript 数组中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com