gpt4 book ai didi

javascript - 转换为键值对

转载 作者:行者123 更新时间:2023-12-03 04:35:20 25 4
gpt4 key购买 nike

我的问题

  1. 如何在JS中读取rom txt文件
  2. 处理数据的最佳方法。像下面这样的东西或者???

    var data = [
    {
    name: "Orange",
    type: "Fruit",
    desc: "some description about the recipe"
    },
    {
    name: "Spinach",
    type: "Veg",
    desc: "some description about the recipe"
    },
    {
    name: "Beans",
    type: "Veg",
    desc: "some description about the recipe"
    },

    ]

我想要一个对象数组,以便我可以进一步处理它以打印出水果的唯一名称,只是蔬菜和水果。

最佳答案

您可以使用 AJAX 和正则表达式来完成此操作。

如果您使用的是 jQuery:

$.ajax({
url: 'someTextFile.txt',
success: function(data) {
var regEx = /([a-zA-Z]+), ([a-zA-Z]+).*[\r\n \t]*([a-zA-Z0-9 \.]+)/g,
recipe, allRecipies = [];
while (recipe = rege.exec(data)) {
allRecipies.push({
name: recipe[1],
type: recipe[2],
desc: recipe[3],
});
}
console.log(allRecipies);
}
});

如果您不使用 jQuery

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
data = xhttp.responseText;
var regEx = /([a-zA-Z]+), ([a-zA-Z]+).*[\r\n \t]*([a-zA-Z0-9 \.]+)/g,
recipe, allRecipies = [];
while (recipe = rege.exec(data)) {
allRecipies.push({
name: recipe[1],
type: recipe[2],
desc: recipe[3],
});
}
console.log(allRecipies);
}
};
xhttp.open("GET", "filename", true);
xhttp.send();

关于javascript - 转换为键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43329409/

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