0

I have the following 2 files, an HTML file and a JSON file called data.txt

In the JSON:

myFunction([
{
"itemid": "0",
"itemname": "air",
"currentprice": "5.0"
},
{
"itemid": "1",
"itemname": "stone",
"currentprice": "5.0"
},
{
"itemid": "2",
"itemname": "grass",
"currentprice": "5.0"
},
{
"itemid": "3",
"itemname": "dirt",
"currentprice": "5.0"
},
{
"itemid": "4",
"itemname": "cobblestone",
"currentprice": "5.0"
},
{
"itemid": "5",
"itemname": "plank",
"currentprice": "5.0"
}
]);

So basically, to my knowledge the only way to modify or view data from this file is with a loop like the following:

<div id="id01"></div>

<script>
function myFunction(arr) {
    var out = "";
    var i;
    for(i = 0; i<arr.length; i++) {
        out += '<a href="' + arr[i].url + '">' + arr[i].itmid + '</a><br>';
    }
    document.getElementById("id01").innerHTML = out;
}
</script>

<script src="data.js"></script>

My questions is this, is there a way to directly edit the values without a loop, possibly similar to editing arrays like itemid[number] = "customValue"

4
  • possible duplicate of Updating a JSON object using Javascript [if the contents of data.txt are saved to a Javascript object using JSON notation] Commented Dec 31, 2014 at 18:24
  • Not entirely clear what your objectives are Commented Dec 31, 2014 at 18:25
  • 1
    It's not a duplicate, that link has a variable in the JSON, I don't. I'm using an array literal as the argument Commented Dec 31, 2014 at 18:30
  • 1
    That's JSONP, not JSON. Commented Dec 31, 2014 at 18:37

3 Answers 3

2

I find it hard to understand what you're asking for. Are you having trouble parsing the JSON file? If so, do:

var str = '{"stuff":[{"itemid": "0","itemname": "air","currentprice": "5.0"},{"itemid": "1","itemname": "stone","currentprice": "5.0"},{"itemid": "2","itemname": "grass","currentprice": "5.0"},{"itemid": "3","itemname": "dirt","currentprice": "5.0"},{"itemid": "4","itemname": "cobblestone","currentprice": "5.0"},{"itemid": "5","itemname": "plank","currentprice": "5.0"}]}';
var json = JSON.parse(str); //str is the json
for (int i = 0; i < json.stuff.length; i++)
{
    //Do whatever with json.stuff[i]. i.e.:
    document.getElementById("id01").innerHTML += json.stuff[i].itemid; // puts each item's ID
}

UPDATE: To save the JSON to a file on your server, you'll need to have a server-sided script. You probably want to send an AJAX HTTP POST request to your script with the JSON string, and then the script will update your file.

Since it's on your computer, you can just save it using JavaScript. After you modifying the json variable, you can do:

var blob = new Blob([JSON.stringify(json)], {type: "text/plain;charset=utf-8"});
saveAs(blob, "thefile.txt");
Sign up to request clarification or add additional context in comments.

9 Comments

I understand, but I'm trying to keep the data in the data.txt file, and not the HTML file.
Where is data.txt located? On your computer? On a server?
Yea everything is in a folder.
@LawrenceLelo Look at my update.
why is there a var str with all the data, when the data is in data.txt
|
0

You can use the array index to hop directly into a specific item.

function myFunction(arr) {
    console.log(arr[0].itemid);
    console.log(arr[0].itemname);
    console.log(arr[0].currentprice);

    console.log(arr[1].itemid);
    console.log(arr[1].itemname);
    console.log(arr[1].currentprice);
}

2 Comments

I think that's exactly what I'm looking for. I already have a function called myFunction which is being used. How would I use your code in a different function name? Is it possible?
@LawrenceLelo You would rename "myFunction" to whatever else you want.
0

If you're using jQuery you can do something like so:

$.grep(a, function(e){ return e.itemid == 1; });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.