在Javascript中,如何将字符串值附加到JSON对象,将其存储到文件中,并在以后检索/操作它?



我想做以下事情;

  1. 将CSV文件加载到程序存储器中,以数组(abc,def,ghi等(进行
  2. 加载。
  3. 用户输入文本并单击"添加"(例如,首先定义,然后 ghi(
  4. 如果找到匹配项,我们创建一个新的JSON数组("def":true,"ghi":true等(。

这是我的代码:

csv = "abc,def,ghi,jkl,mno";
function myFunc(e) {
var myArray = csv.split(',');
console.log(myArray[0]);
var input = document.getElementById("userInput").value;
if (myArray.indexOf(input) > -1) {
//In the array -  So, we create a new file and add JSON content in it - how? - need to save it in this format - "abc": "true", "def": "true" and so on
alert("The input you entered is valid.");
} else {
//Not in the array
alert("The input you entered is not valid.");
}
}
//How do I read the JSON object from the file later and then parse it back as a JSON object variable?
<form>
<div class="group">
<input type="text" required id="userInput">
<span class="highlight"></span>
<span class="bar"></span>
<label>Enter Input:</label>
</div>
<div class="group">
<input class="button" type="submit" value="Submit" onclick="myFunc(this)">
</div>
</form>

这是创建 JSON 对象的一种方法

var obj={'userInput':[]};
function myFunc(e)
{
obj.userInput.push(document.querySelector('#userInput').value)
console.log(obj)
}
<form>   
<div class="group">  
<label>Enter Input:</label>
<input type="text" required id="userInput">
<span class="highlight"></span>
<span class="bar"></span>

</div>
<div class="group">
<input class="button" type="button" value="Submit" onclick="myFunc(this)">
</div>
</form>

最新更新