gpt4 book ai didi

javascript - 通过匹配文本将 json 数据放入数组中

转载 作者:行者123 更新时间:2023-11-28 19:04:46 25 4
gpt4 key购买 nike

我有 json 数据。

[
["Fruit","Lychee Magic","Dusk"],
["Veggies","Long Beans","Rampage"],
["Fruit","Mango Aroma Sweet","Dawn"]
]

我有按钮。按钮文本的第一部分类似于 json 数据。

<button type="button" class="mood">Mango</button>
<button type="button" class="mood">Lychee</button>

如何将按钮文本与 json 数据匹配并将整行放入数组中?

var butTxArr = [];
$("#intoArr").click(function(){
$('.mood').each(function(){
var obj = //how to match?
butTxArr.push(obj);
})
});

异常输出:

butTxArr = ["Fruit","Lychee Magic","Dusk", "Fruit","Mango Aroma Sweet","Dawn"];

最佳答案

类似这样的吗?

$("#intoArr").click(function () {
var butTxArr = [];
$('.mood').each(function () {
// trim button text
var text = $(this).text().replace(/^\s*/g, '').replace(/\s*$/g, '');
data.forEach(function (row) {
// see if the row contains the text
if (row.toString().indexOf(text) !== -1)
// append to array
butTxArr = butTxArr.concat(row);
})
})
// we're done!
console.log(butTxArr)
});

$(document).ready(function() {
var data = [
["Fruit", "Lychee Magic", "Dusk"],
["Veggies", "Long Beans", "Rampage"],
["Fruit", "Mango Aroma Sweet", "Dawn"]
];

$("#intoArr").click(function() {
var butTxArr = [];
$('.mood').each(function() {
// trim button text
var text = $(this).text().replace(/^\s*/g, '').replace(/\s*$/g, '');
data.forEach(function(row) {
// see if the row contains the text
if (row.toString().indexOf(text) !== -1)
// append to array
butTxArr = butTxArr.concat(row);
})
})
// we're done!
alert(butTxArr)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="mood">Mango</button>
<button type="button" class="mood">Lychee</button>

<button id="intoArr">Show</button>

关于javascript - 通过匹配文本将 json 数据放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31917307/

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