Is there a better way other than looping to find data in JSON? It's for edit and delete.
for(var k in objJsonResp) { if (objJsonResp[k].txtId == id) { if (action == 'delete') { objJsonResp.splice(k,1); } else { objJsonResp[k] = newVal; } break; } }
The data is arranged as list of maps. Like:
[ {id:value, pId:value, cId:value,...}, {id:value, pId:value, cId:value,...}, ... ]
I suggest using JavaScript's Array method filter() to identify an element by value. It filters data by using a "function to test each element of the array. Return true to keep the element, false otherwise.." The following function filters the data, returning data for which the callback returns true , i.e. where data.
In addition to using the Google-esque search syntax to find things in your logs, Papertrail can parse a JSON object that appears at the end of a log line. Each line can contain arbitrary string data before the JSON.
We can search for a particular value inside the JSON file using the get() method of ObjectNode class, this method used for accessing the value of a specified field of an object node.
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
(You're not searching through "JSON", you're searching through an array -- the JSON string has already been deserialized into an object graph, in this case an array.)
Some options:
If you're in control of the generation of this thing, does it have to be an array? Because if not, there's a much simpler way.
Say this is your original data:
[ {"id": "one", "pId": "foo1", "cId": "bar1"}, {"id": "two", "pId": "foo2", "cId": "bar2"}, {"id": "three", "pId": "foo3", "cId": "bar3"} ]
Could you do the following instead?
{ "one": {"pId": "foo1", "cId": "bar1"}, "two": {"pId": "foo2", "cId": "bar2"}, "three": {"pId": "foo3", "cId": "bar3"} }
Then finding the relevant entry by ID is trivial:
id = "one"; // Or whatever var entry = objJsonResp[id];
...as is updating it:
objJsonResp[id] = /* New value */;
...and removing it:
delete objJsonResp[id];
This takes advantage of the fact that in JavaScript, you can index into an object using a property name as a string -- and that string can be a literal, or it can come from a variable as with id
above.
(Dumb idea, predates the above. Kept for historical reasons.)
It looks like you need this to be an array, in which case there isn't really a better way than searching through the array unless you want to put a map on it, which you could do if you have control of the generation of the object. E.g., say you have this originally:
[ {"id": "one", "pId": "foo1", "cId": "bar1"}, {"id": "two", "pId": "foo2", "cId": "bar2"}, {"id": "three", "pId": "foo3", "cId": "bar3"} ]
The generating code could provide an id-to-index map:
{ "index": { "one": 0, "two": 1, "three": 2 }, "data": [ {"id": "one", "pId": "foo1", "cId": "bar1"}, {"id": "two", "pId": "foo2", "cId": "bar2"}, {"id": "three", "pId": "foo3", "cId": "bar3"} ] }
Then getting an entry for the id in the variable id
is trivial:
var index = objJsonResp.index[id]; var obj = objJsonResp.data[index];
This takes advantage of the fact you can index into objects using property names.
Of course, if you do that, you have to update the map when you modify the array, which could become a maintenance problem.
But if you're not in control of the generation of the object, or updating the map of ids-to-indexes is too much code and/ora maintenance issue, then you'll have to do a brute force search.
Somewhat OT (although you did ask if there was a better way :-) ), but your code for looping through an array is incorrect. Details here, but you can't use for..in
to loop through array indexes (or rather, if you do, you have to take special pains to do so); for..in
loops through the properties of an object, not the indexes of an array. Your best bet with a non-sparse array (and yours is non-sparse) is a standard old-fashioned loop:
var k; for (k = 0; k < someArray.length; ++k) { /* ... */ }
or
var k; for (k = someArray.length - 1; k >= 0; --k) { /* ... */ }
Whichever you prefer (the latter is not always faster in all implementations, which is counter-intuitive to me, but there we are). (With a sparse array, you might use for..in
but again taking special pains to avoid pitfalls; more in the article linked above.)
Using for..in
on an array seems to work in simple cases because arrays have properties for each of their indexes, and their only other default properties (length
and their methods) are marked as non-enumerable. But it breaks as soon as you set (or a framework sets) any other properties on the array object (which is perfectly valid; arrays are just objects with a bit of special handling around the length
property).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With